简体   繁体   English

按多个类别过滤Wordpress帖子

[英]filter Wordpress Posts by Multiple category

I am sorting post and displaying a single category with this code. 我正在整理帖子并使用此代码显示单个类别。

 global $post;
    $args = array( 
        'category_name'=>'oranges',
        'numberposts'   => -1,
    );

I would like to display all post that are in both "oranges" and "apple" categories so I have modified the code I am using to display a single category to this: 我想显示所有“桔子”和“苹果”类别的帖子,所以我修改了我用来显示单个类别的代码:

global $post;
    $args = array( 
        'category_name'=>'oranges',
        'category_name'=>'apples',
        'numberposts'   => -1,
    );

This displays only the post in the apples category. 这仅显示苹果类别中的帖子。
Thanks 谢谢

<ul>
    <?php
    global $post;

    $myposts = get_posts( array(
        'posts_per_page' => 5,
        'offset'         => 1,
        'category'       => 1 // category id here
    ) );

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

Thanks Arsalan for the suggestion this code here is pulling only the posts in both categories. 感谢Arsalan的建议,此处的代码仅提取两个类别中的帖子。

global $post;
    $args = array( 
        'category_name'=>'oranges + apples',
        'numberposts'   => -1,
    );

Use (+) for multiple categories 使用(+)表示多个类别

global $post;
$args = array( 
    'category_name'=>'oranges + apples',
    'numberposts'   => -1,
);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM