无法显示分类的自定义帖子

时间:2019-09-13 作者:Captain Dando

我有一个名为“brand”的自定义帖子类型,其中包含“brand categories”的分类法。尽管我可能会尝试,但我找不到在当前分类法中显示帖子列表的方法。有人能帮忙吗?

<?php
function List_Brands_of_Category(){
$terms = wp_get_post_terms( $post->ID, \'brand-categories\'); 

foreach ( $terms as $term ) {
    echo "$term->ID"; 
}
$args = array(
    \'post_type\' => \'brand\',
    \'tax_query\' => array(
        \'relation\' => \'AND\',
        array(
            \'taxonomy\' => \'brand-categories\',
            \'field\'    => \'ID\',
            \'terms\'    => array($term->term_id)
        )
    ),
);// end args



$query = new WP_Query($args); ?>
    <?php       
    if ( $query->have_posts() ) {
        while ( $query->have_posts() ) {
            echo \'Brand\';
            $query->the_post(); 
        } 
    } 
    else {?>

    <p><?php _e( \'Sorry, no posts matched your criteria.\' ); 
    }

    ?></p>

<?php 
}

  function DMBrandofCategory_Cleanse() {
     ob_start();
        List_Brands_of_Category();
      return ob_get_clean();
 }
 add_shortcode(\'WC_BOC\', \'DMBrandofCategory_Cleanse\');
 ?>

1 个回复
SO网友:Captain Dando
$args = array(
    \'post_type\' => \'brand\',
    \'tax_query\' => array(
        \'relation\' => \'AND\',
        array(
            \'taxonomy\' => \'brand-categories\',
            \'field\'    => \'name\',
            \'terms\'    => get_queried_object()
        )
    ),
);// end args