2016-12-16 63 views
1

我想在循環的第4個位置顯示最近的帖子。顯示最近的帖子在第4個帖子

例如,我有1到10個帖子,10個帖子是最近發佈的帖子,我想在第4個位置展示它。

這是我的代碼。

<?php 
$blog_arg = array('post_type'=>'post','posts_per_page'=>7,'order'=>'DESC', 'orderby'=> 'most_recent',); 
$get_blogs = new WP_QUERY($blog_arg); 

if($get_blogs->have_posts()){ 
    global $post; 
    $count =1; 
    while($get_blogs->have_posts()) : $get_blogs->the_post(); 
     $category_name = wp_get_post_terms($post->ID, 'category', array("fields" => "names")); 

     if(has_post_thumbnail()){ ?> 
      <div class="blog_posts" style="background:#767b7b url(<?php the_post_thumbnail_url();?>) ; "> 
     <?php 
     }else{ 
      if($count %2 == 0){ 
       echo ' <div class="blog_posts" style="background:#909696">'; 
      }else{ 
       echo ' <div class="blog_posts" style="background:#767b7b">'; 
      } 
     } 

     if($count == 4){ 
      echo "Display Recent post heree.."; 
     } 
    ?> 

      <div class="verti-middal-main"> 
       <h3><?php echo $post->post_title;?></h3> 
       <span><?php echo $category_name[0]; ?></span> 
       <div class="blog-sort-disk"><?php echo wp_trim_words($post->post_content, 20, '');?></div> 
       <a class="read-more" href="<?php the_permalink(); ?>">READ MORE</a> 
      </div> 
      <div id="blog_loading_loader" ></div> 
     </div> 
    <?php 
    $count++; 
    endwhile; 
}?> 

我試過,但在第4位沒有出現最近的文章中這樣我怎麼能顯示在第4位最近的文章(10號後)。

回答

0

您想要在位置4以圖形方式顯示第十個元素嗎?

如果是的話,我的建議是把每個職位的所有元素在自定義陣列和喜歡的東西:

while($get_blogs->have_posts()) : $get_blogs->the_post(); 
    $custom_array_of_posts[]['title'] = $post->post_title; 
endwhile; 
//Then you could use this array and print whenever you need 
// Using a foreach for example 

$custom_array_of_posts[10][title] //Access the title of tenth post 

你可以仔細地控制元素的顯示。

希望得到這個幫助。

+0

如果我的職位數增加,那麼,每次我改變數組的值。 – maddy

+0

也許我不明白你的問題。 你有10個職位,你想打印第四位的第十位? –

相關問題