按多个类别过滤 Wordpress 帖子



我正在对帖子进行排序并使用此代码显示单个类别。

 global $post;
    $args = array( 
        'category_name'=>'oranges',
        'numberposts'   => -1,
    );
我想显示"

橙子"和"苹果"类别中的所有帖子,所以我修改了我用来显示单个类别的代码:

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

这将仅显示苹果类别中的帖子。
谢谢

对多个类别使用 (+)

global $post;
$args = array( 
    'category_name'=>'oranges + apples',
    'numberposts'   => -1,
);
<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>

感谢 Arsalan 的建议,这里的代码只拉取两个类别中的帖子。

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

最新更新