【问题标题】:Wordpress display all posts under specific categoryWordpress 显示特定类别下的所有帖子
【发布时间】:2013-03-08 00:01:24
【问题描述】:

我正在尝试显示子子类别下的所有帖子。但是我的代码继续呈现父类别,然后是所有帖子,然后是子类别和所有帖子。我不断获得更多我想要的类别。此侧边栏仅在 single.php 中使用。

我的分类结构是这样的。

  • 父母
    • 儿童 1
      • 儿童-儿童 1.1
      • 儿童-儿童 1.2
    • 孩子 2
      • Child-Child 2.2

在 Child-child 下我有帖子,我想如何呈现它。

<div id="child-child-title">
    <ul>
        <li class="post">title + post meta data</li>
        <li class="post">title + post meta data</li>
        <li class="post">title + post meta data</li>
    </ul>
</div>

这是我正在使用的,它正在渲染每个类别(无论是父级还是子级)以及该站点下的每个帖子。

<?php global $post; $categories = get_the_category();foreach ($categories as $category) : ?>
<h4>&raquo; <?php echo $category->name; ?></h4>
<ul>

<?php
foreach((get_the_category()) as $category) {
    $cat_ids=$cat_ids . $category->cat_ID . ',';
}
$cat_ids=substr($cat_ids, 0, -1);
//----------
?>

<?php $posts = get_posts('numberposts=20&amp;category='. $cat_ids);foreach($posts as $post) : ?>
<a href='<?php the_permalink() ?>'><?php the_title(); ?></a></li><br>
<?php endforeach; ?>
<li><strong><a href="<?php echo get_category_link($category->term_id);?>"><?php echo $category->name; ?> </a></strong></li>
<?php endforeach; ?>
</ul>

提前致谢!

【问题讨论】:

    标签: php wordpress list categories


    【解决方案1】:

    使用来自特定类别的查询帖子

    query_posts( array ( 'category_name' => 'my-category-slug', 'posts_per_page' => -1 ) );
    

    有关使用此功能的更多参考,请访问Category Function reference

    但是,对于子类别(或子类别),“类别名称”并不总是有效。而是使用'category-slug'。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-06-24
      相关资源
      最近更新 更多