【问题标题】:How can i get all post of taxonomy category post by taxonomy category id or taxonomy category name in wordPress?如何在 wordPress 中通过分类类别 ID 或分类类别名称获取分类类别帖子的所有帖子?
【发布时间】:2014-11-15 08:26:20
【问题描述】:

我创建了帖子类型的产品,还创建了名为 productcategory 的分类法。 现在我需要在相关产品中获得特定类别。 不知何故,我设法通过使用

找到了分类类别 ID 和名称
$term_list = wp_get_post_terms($post->ID, 'productcategory', array("fields" => "ids"));

现在我怎样才能得到这个分类的所有帖子?

【问题讨论】:

标签: php wordpress


【解决方案1】:

显示与特定分类相关的帖子。如果指定注册到自定义帖子类型的分类,那么您将使用“{custom_taxonomy_name}”而不是使用“类别”。例如,如果您有一个名为“流派”的自定义分类,并且只想显示来自“爵士”流派的帖子,则可以使用以下代码。

 <?php
$args = array(
     'posts_per_page' => 8,
     'orderby' => 'rand',
     'post_type' => 'products',
     'productcategory' => 'your_cat_name',
     'post_status' => 'publish'
);
$show_albums = get_posts( $args );
?>

【讨论】:

    【解决方案2】:
    $terms = get_the_terms( $post->ID, 'productcategory' );    
    
    $args = array(
    'post_type' => 'products',
    'tax_query' => array(
        array(
            'taxonomy' => 'productcategory',
            'field'    => 'slug',
            'terms' => explode(',',$terms),
        ),
    ),
    );
    $query = new WP_Query( $args );
    

    然后只需使用此查询的结果运行一个循环,您就可以开始了!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-04-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-05
      • 2013-06-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多