Wordpress显示类别ID的帖子

Wordpress显示类别ID的帖子,wordpress,parent,categories,posts,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="tim

似乎找不到正确的答案,我认为这是微不足道的

我有一些分类是这样安排的

父类别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>

正如您所见,我必须手动指定类别(cat=2),但我希望它自动检测我已经在的类别并显示帖子(这样,如果我在不同的类别中,它将显示这些帖子)

有什么建议吗


提前谢谢。(SO Community=Awesome saint)。

如果您使用的是
category.php
,您可以省略
query\u posts
,它将自动为您填写帖子。

尝试以下代码:

<?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>

试试看


试试这个,这是一个更好的解决方案,你也可以用它来显示一个类别id相关的文章

只需使用此行调用下面提到的函数。将其放入模板或page.php/single.php文件中

通过put此行调用:
相关帖子标题('在此处输入cat id…)

这是函数并将其放入
Function.php
文件中

相关职位职能:

function related_post_title($cat_id){

    $cat = $cat_id;

    // Check if it is page only
    if ( is_page() || is_single()) {
        $args=array(
            'cat'  =>  $cat,
            'order'  =>  DESC,
            'orderby'  =>  rand,
            'post__not_in'  =>  array($post->ID),
            'posts_per_page'  =>  9999,
            'caller_get_posts'  =>  1
            );

        $my_query = null;

        $my_query = new WP_Query($args);

        if( $my_query->have_posts() ) {

            echo  ' <ul> ';

            while ($my_query->have_posts()) : $my_query->the_post(); ?>

                <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>

            <?php

            endwhile;

            echo '</ul>';
        }
        wp_reset_query();
    }
}
功能相关的帖子标题($cat\u id){
$cat=$cat\u id;
//检查是否仅为第页
if(is_page()| | is_single()){
$args=数组(
“猫”=>$cat,
“订单”=>DESC,
'orderby'=>rand,
'post\u not\u in'=>数组($post->ID),
“每页帖子数”=>9999,
“来电者获取帖子”=>1
);
$my_query=null;
$my\u query=新的WP\u查询($args);
如果($my\u query->have\u posts()){
回声“
    ”; while($my_query->have_posts()):$my_query->the_post();?>
  • 
    
    我认为您有一个错误:'posts\u per\u page'=>$showposts\u不,这应该是…'posts\u per\u page'=>$showposts,Anyhoo,它显示所有帖子,而不仅仅是该类别的帖子:-(另外,如果我在循环中抛出echo$current_cat_id;,它不会按预期显示类别id,因此我只能假设它没有实际获取。谢谢sandip。你节省了我的时间。你为什么不使用它?你说你想“显示我当前所在类别的所有帖子”。尝试实现这一目标的最佳实践是使用
    category.php
    (和子文件),如Codex中所述。
    $args = array( 'posts_per_page' => 5, 'offset'=> 0, 'cat' => 1 );
    
    function related_post_title($cat_id){
    
        $cat = $cat_id;
    
        // Check if it is page only
        if ( is_page() || is_single()) {
            $args=array(
                'cat'  =>  $cat,
                'order'  =>  DESC,
                'orderby'  =>  rand,
                'post__not_in'  =>  array($post->ID),
                'posts_per_page'  =>  9999,
                'caller_get_posts'  =>  1
                );
    
            $my_query = null;
    
            $my_query = new WP_Query($args);
    
            if( $my_query->have_posts() ) {
    
                echo  ' <ul> ';
    
                while ($my_query->have_posts()) : $my_query->the_post(); ?>
    
                    <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
    
                <?php
    
                endwhile;
    
                echo '</ul>';
            }
            wp_reset_query();
        }
    }
    
        <?php
        // Start the loop.
        $categories = get_categories('child_of=1');//Parents category id
                            foreach ($categories as $cat) {
    
                                $option = '<a href="/category/archives/'.$cat->category_nicename.'">';
                                $option .= $cat->cat_name;//parents sub category name
                                $option .= '</a>';
                                echo $option;
                                query_posts('cat=$cat->cat_ID&showposts=10');
    if (have_posts()) : while (have_posts()) : the_post(); ?>      
    
    <h3><?php the_title(); ?></h3>
    <?php the_content();?>
    <?php endwhile; else: ?>
    <?php _e('No Posts Sorry.'); ?>
    <?php endif;  }?>