Wordpress显示类别ID的帖子

Mik*_*ker 0 wordpress parent categories posts

似乎无法找到我认为琐碎的正确答案.

我有一些像这样排列的类别......

父母类别1
- 儿童类别1
- 儿童类别2
- 儿童类别3

...我有一些儿童类别2的帖子.我希望我的页面显示我目前所在类别的所有帖子.

这就是我现在正在做的事情:

<?php
query_posts('cat=2&showposts=10');
    if (have_posts()) : while (have_posts()) : the_post(); ?>      
    <div class="timeline">
    <h3><?php the_title(); ?></h3>
    <?php the_content();?>
    <?php endwhile; else: ?>
    <?php _e('No Posts Sorry.'); ?>
    <?php endif; ?>
</div>
Run Code Online (Sandbox Code Playgroud)

正如您所看到的,我必须手动指定类别(cat = 2),但我希望它能够自动检测我已经在的类别并显示帖子(如果我在不同的类别中它会显示那些帖子).

有什么建议?

提前致谢.(SO社区=很棒的酱).

小智 8

尝试以下代码:

<?php
$current_cat_id  = get_query_var('cat');
$showposts = 10;
$args = array('cat' => $current_cat_id, 'orderby' => 'post_date', 'order' => 'DESC', 'posts_per_page' => $showposts,'post_status' => 'publish');
query_posts($args);
    if (have_posts()) : while (have_posts()) : the_post(); ?>      
    <div class="timeline">
    <h3><?php the_title(); ?></h3>
    <?php the_content();?>
    <?php endwhile; else: ?>
    <?php _e('No Posts Sorry.'); ?>
    <?php endif; ?>
</div>
Run Code Online (Sandbox Code Playgroud)