列表&计数外观

时间:2018-02-19 18:46:47

标签: php wordpress

我想要做的是列出演员在电影中出现的电影数量。 我尝试了几种方法,但无济于事。

<?php foreach (get_terms('actor') as $cat) : ?>

            <div class="col-md-6">

                <?php $the_query = new WP_Query( array(
'post_type' => 'post',
'tax_query' => array(
    array(
        'taxonomy' => 'actor',
        'field' => 'slug',
        'terms' => 'movie',
    )
)
) );
$count = $the_query->found_posts; ?>
<?php echo $cat->name; ?> [<?php echo $count; ?>]
</div>
<?php endforeach; ?>

我有自定义的帖子类型电影和自定义分类法演员。

我知道我很亲密......

提前致谢

1 个答案:

答案 0 :(得分:1)

这对我有用:

<?php
// Change these if necessary. E.g. If taxonomy name is 'actors' and not 'actor'.
$post_type = 'movie';
$taxonomy = 'actor';

foreach ( get_terms( array(
    'taxonomy' => $taxonomy,
) ) as $cat) :
?>
    <div class="col-md-6">
        <?php $the_query = new WP_Query( array(
            'post_type' => $post_type,
            'tax_query' => array(
                array(
                    'taxonomy' => $taxonomy,
                    'field' => 'term_id',
                    'terms' => $cat->term_id,
                )
            )
        ) );
        $count = $the_query->found_posts; ?>
        <?php echo $cat->name; ?> [<?php echo $count; ?>]
    </div>
<?php endforeach; ?>

示例输出:

enter image description here