【问题标题】:Wordpress latest posts featured image from categoryWordpress 最新帖子 分类中的精选图片
【发布时间】:2015-05-03 17:01:55
【问题描述】:

快速 Wordpress 问题。

我想显示我的类别“照片”中的最后 30 个帖子,但只在相关页面上显示特色图片作为链接,将用户带到实际帖子。

我已经做到了,但它显示了所有类别的帖子,而不是“照片”类别。我正在使用的代码如下。

我确信这很简单,但我很想知道如何仅显示照片类别中的最新帖子(作为特色图片)。

谢谢

<!-- In functions.php -->
function recentPosts() {
	$rPosts = new WP_Query();
	$rPosts->query('showposts=100');
		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(); ?>

【问题讨论】:

    标签: wordpress


    【解决方案1】:

    您需要通过提供类别 ID cat=1 来提供您只想发布特定类别的循环参数。将 1 替换为您的 photos category 的 id

    <!-- 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(); ?>
    

    【讨论】:

      【解决方案2】:

      只需将类别选择添加到您的查询中。

      替换:

      $rPosts = new WP_Query();
      $rPosts->query('showposts=100');
      

      与:

      $rPosts = new WP_Query('category_name=photos&showposts=100');
      

      参考:http://codex.wordpress.org/The_Loop#Exclude_Posts_From_Some_Category

      【讨论】:

        【解决方案3】:

        将类别 ID 添加到您的查询参数中。顺便说一句,最后一行的echo 是多余的。您的函数直接输出 HTML 而不是返回它。

        最后您的原始标记无效。 li 不能是 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(); ?>
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-11-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多