如何在Wordpress中打印多个类别?

时间:2018-08-07 00:00:53

标签: wordpress custom-post-type

我想通过简码传递多个类别ID,它应该打印该类别中的所有帖子。

例如[myshortcode cat =“ 1,2”]

但是我遇到的问题是它只打印第一类的帖子。我想我无法遍历类别ID。

这是我的代码。

// define attributes
extract( shortcode_atts( array (
    'cat' => '',
    'orderby' => 'date',
), $atts ) );

分隔IDS

 if($cat) {
        $args['category__in'] = array_map('intval',explode(',',$cat));
    }

显示帖子

 $query = new WP_Query( $args );
    if( $query->have_posts() ){
        echo "<div class='row col-div investment-guides'>";
        while( $query->have_posts() ){

            $query->the_post();
            $id = get_the_ID();
            $page = get_post($id);
            $image = get_the_post_thumbnail_url($id);


           display_layout($page,$image); //calls the function below
        }
        echo '</div>';
    }

显示单个帖子的功能

function display_layout($page,$image) {

    echo "<div class='col-sm-6  pair-cards'>
            <div class='sub-div'>
                <div class='card-img'>
                <a href='$page->post_name'><img src='$image' alt='$page->post_name'  /></a>
                </div>".
                    "<div class='entry-title'><a href='$page->post_name'>" .  $page->post_title  .  "</a></div>
                <div class='card-desc'>".
                    "<p>" . strip_tags($page->post_excerpt) ."</p>
                </div>
                <div class='read-more'>
                    <a href='$page->post_name'> Read More </a>
                </div>
            </div>
        </div>";
}

var_dump($ args)

 'posts_per_page' => string '-1' (length=2)
  'orderby' => null
  'category__in' => 
    array (size=2)
      0 => int 1
      1 => int 2

var_dump($ query)

object(WP_Query)[1984]
  public 'query' => 
    array (size=3)
      'posts_per_page' => string '-1' (length=2)
      'orderby' => null
      'category__in' => 
        array (size=2)
          0 => int 1
          1 => int 2
  public 'query_vars' => 
    array (size=65)
      'posts_per_page' => int -1
      'orderby' => null
      'category__in' => 
        array (size=2)
          0 => int 1
          1 => int 2

我们将不胜感激。

0 个答案:

没有答案