试图从另一个自定义帖子获取分类链接

时间:2017-01-16 13:39:09

标签: php wordpress taxonomy

我在自定义帖子类型'列表'上列出所有电影,并且当我试图为电影导演获取分类链接时,所有导演列在自定义帖子类型'导演'上。

<?php

$taxonomy ='mydirectors';
$term_lists = get_the_terms($post->ID, $taxonomy);
foreach ($term_lists as $termlist):
    $name_term= $termlist->slug;
    $argsdirectors = array(
        'post_type' => 'directors',
        'name' => $name_term
        );
    $loop = new WP_Query($argsdirectors);
    if ( $loop->have_posts() ) :
        while($loop->have_posts()): $loop->the_post();
    ?>  <a href="<?php the_permalink() ?>">   <?php 
    $terms = get_the_terms( $post->ID , 'mydirectors' );
    if ( $terms != null ){
        print ' <div class="detail"><span class="name"><font size="+1">Director</font></span><br />';
        foreach( $terms as $term ) {
            print $term->name."<br>" ;
            unset($term);
            if (!empty($terms)) {
                echo "";
            }
        }
        print "</div><br>";
    } 
    ?></a>
    <?php
    endwhile;
    endif;
    endforeach;
    wp_reset_query();
    ?>

有什么问题?

1 个答案:

答案 0 :(得分:0)

尝试使用get_the_ID()替换$ post-&gt; ID,如下所示:

$terms = get_the_terms( get_the_ID() , 'mydirectors' );

参考:https://codex.wordpress.org/Function_Reference/get_the_ID