2015-03-03 80 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(); ?>