【问题标题】:filter Wordpress Posts by Multiple category按多个类别过滤 Wordpress 帖子
【发布时间】:2017-02-02 18:22:15
【问题描述】:

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

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

我想显示同时属于“橙子”和“苹果”类别的所有帖子,因此我修改了用于显示单个类别的代码:

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

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

【问题讨论】:

  • 试试'category_name'=>'oranges,apples''category_name'=>'oranges+apples'
  • 感谢 Arsalan 成功了。

标签: php wordpress


【解决方案1】:

对多个类别使用 (+)

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

【讨论】:

    【解决方案2】:
    <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>
    

    【讨论】:

      【解决方案3】:

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

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-08-19
        • 1970-01-01
        • 2017-06-12
        • 1970-01-01
        • 2019-10-01
        • 2012-06-19
        相关资源
        最近更新 更多