0

这段代码loop-ad_listing.php使所有帖子结果都显示在列表中。但现在我想将特色帖子放在非特色帖子之上(顺序 = 特色,然后是非特色)。我需要在此代码中输入什么代码才能使特色帖子首先出现?

任何帮助表示赞赏:)

 <?php if ( have_posts() ) : ?>

<div id="page-<?php echo $paged ?>">


    <?php while ( have_posts() ) : the_post(); ?>

        <?php
        if ( get_post_meta($post->ID, 'location', true) )
            $make_address = get_post_meta($post->ID, 'location', true);
        else
            $make_address = get_post_meta($post->ID, 'cp_street', true) . '&nbsp;' . get_post_meta($post->ID, 'cp_city', true) . '&nbsp;' . get_post_meta($post->ID, 'cp_state', true) . '&nbsp;' . get_post_meta($post->ID, 'cp_zipcode', true);
        ?>

        <div id="post-id-<?php the_ID(); ?>" class="post-wrapper">

            <div class="post-block">
                <?php if ( is_sticky() ) echo '<strong>FEATURED</strong>'; ?>

             <div class="grid_100">
             <?php if ( get_option('cp_ad_images') == 'yes' ) cp_ad_loop_thumbnail(); ?>

                <h3 class="post-title"><a href="<?php the_permalink(); ?>"><?php if ( mb_strlen( get_the_title() ) >= 75 ) echo mb_substr( get_the_title(), 0, 75 ).'...'; else the_title(); ?></a></h3>
        </div>
        <div class="post-desc">
        <?php echo $make_address; ?>
                    <div class="post-debug">

                        <!--<h4>Description:</h4>
                        <?php echo cp_get_content_preview( 160 ); ?>--!>
                    </div>
                </div>
            </div><!-- /post-block -->
        </div><!-- /post-wrapper -->

    <?php endwhile; ?>

</div><!-- /page --> 

<?php else: ?>

    <div class="block"><center>Sorry, no results found...</center></div>

<?php endif; ?>
4

2 回答 2

0

You can use query_posts() to before your Loop to orderby the meta_value using the specified meta_key. This assumes that cp_ad_featured_thumbnail is not set for un-featured posts.

<?php
$args = array( "orderby"=>"meta_value", "meta_key"=>"cp_ad_featured_thumbnail" );
query_posts( $args );
?>
<?php if ( have_posts() ) : ?> 
<div id="page-<?php echo $paged ?>"> 
<?php while ( have_posts() ) : the_post(); ?>
    <!-- POST CONTENT -->
<?php endwhile; endif; ?>
于 2013-05-20T17:52:26.640 回答
0
<?php 
<!-- display all post (exclude non-sticky post) -->
query_posts(array("post__in"=>get_option("sticky_posts")));

if ( have_posts() ) : ?>

<div id="page-<?php echo $paged ?>">


<?php while ( have_posts() ) : the_post(); ?>

    <?php
    if ( get_post_meta($post->ID, 'location', true) )
        $make_address = get_post_meta($post->ID, 'location', true);
    else
        $make_address = get_post_meta($post->ID, 'cp_street', true) . '&nbsp;' . get_post_meta($post->ID, 'cp_city', true) . '&nbsp;' . get_post_meta($post->ID, 'cp_state', true) . '&nbsp;' . get_post_meta($post->ID, 'cp_zipcode', true);
    ?>

    <div id="post-id-<?php the_ID(); ?>" class="post-wrapper">

        <div class="post-block">
          <?php echo '<strong>FEATURED</strong>'; ?>
          <div class="grid_100">
          <?php if ( get_option('cp_ad_images') == 'yes' ) cp_ad_loop_thumbnail(); ?>

            <h3 class="post-title"><a href="<?php the_permalink(); ?>"><?php if ( mb_strlen( get_the_title() ) >= 75 ) echo mb_substr( get_the_title(), 0, 75 ).'...'; else the_title(); ?></a></h3>
          </div>

          <div class="post-desc">
          <?php echo $make_address; ?>
            <div class="post-debug">
              <!--<h4>Description:</h4>
              <?php echo cp_get_content_preview( 160 ); ?>--!>
            </div>
          </div>
        </div><!-- /post-block -->
    </div><!-- /post-wrapper -->

<?php endwhile; ?>

</div><!-- /page --> 

<?php endif; ?>    
<?php 
<!-- display all post (exclude non-sticky post) -->
query_posts(array("post__not_in"=>get_option("sticky_posts")));

if ( have_posts() ) : ?>

<div id="page-<?php echo $paged ?>">


<?php while ( have_posts() ) : the_post(); ?>

    <?php
    if ( get_post_meta($post->ID, 'location', true) )
        $make_address = get_post_meta($post->ID, 'location', true);
    else
        $make_address = get_post_meta($post->ID, 'cp_street', true) . '&nbsp;' . get_post_meta($post->ID, 'cp_city', true) . '&nbsp;' . get_post_meta($post->ID, 'cp_state', true) . '&nbsp;' . get_post_meta($post->ID, 'cp_zipcode', true);
    ?>

    <div id="post-id-<?php the_ID(); ?>" class="post-wrapper">

        <div class="post-block">

          <div class="grid_100">
          <?php if ( get_option('cp_ad_images') == 'yes' ) cp_ad_loop_thumbnail(); ?>

            <h3 class="post-title"><a href="<?php the_permalink(); ?>"><?php if ( mb_strlen( get_the_title() ) >= 75 ) echo mb_substr( get_the_title(), 0, 75 ).'...'; else the_title(); ?></a></h3>
          </div>

          <div class="post-desc">
          <?php echo $make_address; ?>
            <div class="post-debug">
              <!--<h4>Description:</h4>
              <?php echo cp_get_content_preview( 160 ); ?>--!>
            </div>
          </div>
        </div><!-- /post-block -->
    </div><!-- /post-wrapper -->

<?php endwhile; ?>

</div><!-- /page --> 

<?php else: ?>

<div class="block"><center>Sorry, no results found...</center></div>

<?php endif; ?>
于 2013-05-23T02:25:44.280 回答