【问题标题】:Show a list of posts in category page Wordpress在类别页面 Wordpress 中显示帖子列表
【发布时间】:2021-12-22 17:37:17
【问题描述】:

所以基本上我正在尝试显示类别页面的帖子列表。我需要动态地做,所以每个类别页面都有自己的帖子列表。我最终得到了这段代码,但它显示了所有类别的所有帖子。

<?php

$lastposts = get_posts(array(
    'posts_per_page' => 5
));

if ($lastposts)
{
    foreach ($lastposts as $post):
        setup_postdata($post); ?>
        <h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
        
    <?php
    endforeach;
    wp_reset_postdata();
}
?>

【问题讨论】:

  • 所以将category 参数传递给get_posts 然后...?
  • 例如,如果我在此页面中有 site.com/category/category-x,我只想显示与 category-x 相关的帖子列表,而不是像您使用参数 the_content( );.使用我发布的代码,它显示了所有类别的所有帖子
  • 是的,因为您尚未指定您希望帖子来自哪些类别。这就是我告诉你这就是你应该做的原因......

标签: php wordpress wordpress-theming custom-wordpress-pages


【解决方案1】:

我刚刚找到了解决办法

<div class="entry-content">
        <div class=" my-5">         
            <h2><a href="<?php the_permalink(); ?>" class="section-heading__title" rel="noopener">
                <?php the_title(); ?>
            </a></h2>
            <div>
                <div class="attachment__description my-2"><?php echo get_the_excerpt(); ?></div>
                <hr/>
                <span class="uppercase">Published on <?php echo get_the_date(); ?></span>
                <span><?php echo site_posted_by();?></span>
            </div>
        </div>
</div><!-- .entry-content -->

【讨论】:

  • 您的答案可以通过额外的支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以找到更多关于如何写好答案的信息in the help center
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-04-09
  • 1970-01-01
  • 2016-08-13
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多