2012-12-22 22 views
0

我有一個精選的滑塊爲我的Wordpress平臺,我想要一個特定的帖子首先顯示在query_posts列表中。爲了解釋這一點,我query_posts在自定義分類top=billboard而且我想顯示應在代碼中被硬編碼後的第一個帖子,讓我們說這有ID 70在query_posts的返回列表中添加一個特定的帖子 - WordPress的

下面是代碼的相關部分:

<div id="featured" class="<?php if ($responsive) echo 'flexslider' . $featured_auto_class; else echo 'et_cycle'; ?>"> 
    <a id="left-arrow" href="#"><?php esc_html_e('Previous','Aggregate'); ?></a> 
    <a id="right-arrow" href="#"><?php esc_html_e('Next','Aggregate'); ?></a> 

<?php if ($responsive) { ?> 
    <ul class="slides"> 
<?php } else { ?> 
    <div id="slides"> 
<?php } ?> 
     <?php 
     $arr = array(); 
     $i=0; 

     $featured_cat = get_option('aggregate_feat_cat'); 
     $featured_num = (int) get_option('aggregate_featured_num'); 

     if (get_option('aggregate_use_pages') == 'false') query_posts("showposts=$featured_num&top=billboard"); 
     else { 
      global $pages_number; 

      if (get_option('aggregate_feat_pages') <> '') $featured_num = count(get_option('aggregate_feat_pages')); 
      else $featured_num = $pages_number; 

      query_posts(array 
          ('post_type' => 'page', 
          'orderby' => 'menu_order', 
          'order' => 'ASC', 
          'post__in' => (array) get_option('aggregate_feat_pages'), 
          'showposts' => (int) $featured_num 
         )); 
     } ?> 
     <?php if (have_posts()) : while (have_posts()) : the_post(); 
     global $post; ?> 
     <?php if ($responsive) { ?> 
      <li class="slide"> 
     <?php } else { ?> 
      <div class="slide"> 
     <?php } ?> 
       <?php 
       $width = $responsive ? 960 : 958; 
       $height = 340; 
       $small_width = 95; 
       $small_height = 54; 
       $titletext = get_the_title(); 

       $thumbnail = get_thumbnail($width,$height,'',$titletext,$titletext,false,'Featured'); 

       $arr[$i]['thumbnail'] = get_thumbnail($small_width,$small_height,'',$titletext,$titletext,false,'Small'); 
       $arr[$i]['titletext'] = $titletext; 

       $thumb = $thumbnail["thumb"]; 
       print_thumbnail($thumb, $thumbnail["use_timthumb"], $titletext, $width, $height, ''); ?> 
       <div class="featured-top-shadow"></div> 
       <div class="featured-bottom-shadow feat<?php $category = get_the_category(); echo $category[0]->category_nicename; ?>"></div> 
       <div class="featured-description"> 
        <div class="feat_desc"> 
         <h2 class="featured-title"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2> 
         <p><?php $excerpt = get_the_excerpt(); echo string_limit_words($excerpt,20); ?></p> 
        </div> 
       </div> <!-- end .description --> 
     <?php if ($responsive) { ?> 
      </li> <!-- end .slide --> 
     <?php } else { ?> 
      </div> <!-- end .slide --> 
     <?php } ?> 
     <?php $i++; endwhile; endif; wp_reset_query(); ?> 
<?php if ($responsive) { ?> 
    </ul> <!-- end .slides --> 
<?php } else { ?> 
    </div> <!-- end #slides --> 
<?php } ?> 
</div> <!-- end #featured --> 

我來最接近的是添加以下代碼:

<?php if (have_posts()) : while (have_posts()) : 
    global $post; 
    if (!$first_time) // START custom first post 
    { 
     $post_id = 70; // This is the ID of the first post to be displayed on slider 
     $post = get_post($post_id); 
     $first_time = 1; 
    } 
    else the_post(); // END custom first post 
    ?> 

它的工作原理部分。它首先顯示帖子ID 70,這是好的,但是,似乎爭奪其餘的名單。

所以,我想在返回的列表是按以下順序:

帖子ID 70

帖子來自分類頂部=廣告牌

帖子來自分類頂部=廣告牌

來自分類學的帖子頂部=廣告牌

來自分類學的帖子頂部=廣告牌

...

我怎樣才能達到這個正常嗎?

回答

0

我認爲rewind_posts();應該在ID=70被提取後使用,確保當它在循環中被再次提取時排除它。也就是說,如果我理解正確。

修訂

使用rewind_posts()wp_reset_query()對於這個問題,是後或在循環中的一個選項,但後70外循環中獲取與get_post(),所以有可能在開始循環之前得到它。例如:

$PostID = 70; 
$Post = get_post($PostID); 
echo var_dump($Post); 

// Query 
// Loop 
// Skip post 70 
... 

您正在使用的功能get_post()內循環,這就是爲什麼順序被修改。

+0

我在哪裏使用'rewind_posts();'? –

+0

不在哪裏。這是我的觀點。 –

+0

我的歉意,我明白你現在的意思。調用ID後我添加了rewing_posts,但沒有發生任何事情。你能否更確切地告訴我應該在哪裏添加它?也許我在這裏做錯了... –

相關問題