如何使用WordPress显示与特定分类相关的自定义帖子类型

时间:2016-05-04 08:27:01

标签: php wordpress

我正在尝试在WP中显示特定分类的所有自定义帖子类型。

以下是代码:

<?php

$args = array(
    'post_type' => 'team',
    'orderby'   => 'rand',
    'posts_per_page' => -1
);

$trit = new WP_Query($args);

while ($trit->have_posts()) : $trit->the_post();
    $post_id = get_the_ID();//POST ID
    $terms = get_the_terms($post->ID, 'tax_members' );
    ...
endwhile;

上面的脚本显示所有分类法的所有成员( tax_members )。我的目标是仅显示特定类别的成员...例如:玩家

如何调用

中的分类标准玩家
$terms = get_the_terms($post->ID, 'tax_members' );

2 个答案:

答案 0 :(得分:1)

$args = array(
    'post_type' => 'team',
    'orderby'   => 'rand',
    'posts_per_page' => -1,
    'tax_query' => array(
        array(
            'taxonomy' => 'tax_members',
            'terms'    => array('players'),
            )
        ) 
    );

答案 1 :(得分:1)

<?php
            $queryArr = array(
            'post_type'        => 'team',
            'orderby'          => 'rand',
            'post_status'      => 'publish',
            'posts_per_page'   => -1,
            'tax_query' => array(
                        array(
                                'taxonomy' => 'tax_members',
                                'field'    => 'slug',
                                'terms'    => 'players',
                                'operator' => 'IN'
                            ),
                        ),
            );
            $trit = get_posts( $queryArr );
?>

试试这个