2015-03-03 111 views
0

快速WordPress的問題。WordPress的最新帖子精選圖像

我想顯示我的類別「照片」中的最後30個帖子,但僅在相關頁面上顯示特色圖片,作爲將用戶轉到實際帖子的鏈接。

我設法做到了這一點,但它顯示所有類別的帖子,而不是「照片」類別。我正在使用的代碼如下。

我確定它很簡單,但很想知道如何僅顯示來自照片類別的最近帖子(作爲特色圖片)。

感謝

<!-- In functions.php --> 
 
function recentPosts() { 
 
\t $rPosts = new WP_Query(); 
 
\t $rPosts->query('showposts=100'); 
 
\t \t while ($rPosts->have_posts()) : $rPosts->the_post(); ?> 
 
\t \t <div class="photos"> 
 
\t \t \t <li class="recent"> 
 
\t \t \t \t <a href="<?php the_permalink();?>"><?php the_post_thumbnail('recent-thumbnails'); ?></a> 
 
\t \t \t </li> \t 
 
\t \t </div> 
 
\t \t <?php endwhile; 
 
\t wp_reset_query(); 
 
} 
 

 

 
<!-- this is on the page template --> 
 
<?php echo recentPosts(); ?>

回答

0

您需要提供您希望通過提供類別ID cat=1只張貼的某一類的循環論證。將1替換爲您的ID photos category

<!-- In functions.php --> 
function recentPosts() { 
    $rPosts = new WP_Query(); 
    $rPosts->query('showposts=100&cat=1'); 
     while ($rPosts->have_posts()) : $rPosts->the_post(); ?> 
     <div class="photos"> 
      <li class="recent"> 
       <a href="<?php the_permalink();?>"><?php the_post_thumbnail('recent-thumbnails'); ?></a> 
      </li> 
     </div> 
     <?php endwhile; 
    wp_reset_query(); 
} 


<!-- this is on the page template --> 
<?php echo recentPosts(); ?> 
0

類別ID添加到您的查詢參數。最後一行上的echo是多餘的。你的函數直接輸出HTML而不是返回它。

最後您的原始標記無效。一個李不能是一個div的孩子,所以我在我的例子中糾正了這個問題。

function recentPosts() { 
    $rPosts = new WP_Query(array(
     'posts_per_page' => 30, 
     'cat'   => 1 
     'no_found_rows' => true // more efficient way to perform query that doesn't require pagination. 
    )); 

    if ($rPosts->have_posts()) : 
     echo '<ul class="photos">'; 

     while ($rPosts->have_posts()) : $rPosts->the_post(); ?> 
      <li class="recent"> 
       <a href="<?php the_permalink(); ?>"><?php the_post_thumbnail('recent-thumbnails'); ?></a> 
      </li> 
     <?php endwhile; 

     echo '</ul>'; 
    endif; 

    // Restore global $post. 
    wp_reset_postdata(); 
} 


<!-- this is on the page template --> 
<?php recentPosts(); ?>