如何获得自定义分类法项的术语(post_tag) ?Wordpress


How can I get terms (post_tag) of custom taxonomy item? Wordpress

我创建了自定义帖子类型'photo'和自定义分类'catphoto'。

类型'photo'支持'catphoto'和'post_tag'作为分类法。

现在我应该为客户端创建一个过滤器。

我需要在taxonomy-catphoto.php页面上显示属于当前'catphoto'项目的所有post_tags

例如,我有一个自定义帖子类型'photo'的帖子。这篇文章的名字是"飞机"。这篇文章属于"1961-1981"catphoto条目。也有文章标签像"空间","飞机"、"明星","战争"。

另外,例如,我有一个名为"士兵"的"照片"帖子。它属于'1941-1961' catphoto项目,并具有'二战', '美国', '苏联'类似post标签。

现在,当我选择1941-1961 catphoto项目时,我应该得到一个列表:二战美国苏联

我试着这样做:

if (substr($_SERVER['REQUEST_URI'],1,8) == 'catphoto'){
    $cur_terms = get_terms('post_tag');
    if ($cur_terms) {
        foreach( $cur_terms as $cur_term ){
            echo $cur_term->name.'<br/>';
        }
    }
}

现在我得到了所有catphoto项目的所有post标签。我怎样才能为确定的照片项目设置限制呢?例如,'1941-1961' ?

我已经通过$_GET查询解决了这个问题。那么容易…首先,我决定创建自己的分类法类型(如post_tags)。它的名字是'mark'。

之后的代码片段是:

if (substr($_SERVER['REQUEST_URI'],1,8) == 'catphoto'){
    $term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) ); 
    $query = new WP_Query( array('post_type' => 'photo', 'catphoto' => $term->name));
    echo '<ul>';
    $uniqueTerms = array();
    $uniqueSlugs = array();
    $uniquePar = $term->slug;
    while ( $query->have_posts() ) { 
        $query->the_post();
        $terms = wp_get_post_terms(get_the_ID(), 'mark', array("fields" => "all"));
        foreach ($terms as $term1)
        {
            if(!in_array($term1->name, $uniqueTerms)) {
               $uniqueTerms[] = $term1->name;
               $uniqueSlugs[] = $term1->slug; 
            }
        }   
    } 
    for ($var = 0; $var < count($uniqueTerms); $var++)
        echo '<li><a href="'.esc_url( home_url( '/catphoto/' ) ).$uniquePar.'/?mark='.$uniqueSlugs[$var].'">'.$uniqueTerms[$var].'</a></li>';
    echo '</ul>';
}

到处都是丑陋的代码,但它可以工作。

,现在在taxonomy-catphoto.php我填充:

$mark = $_GET['mark'];

之后在WP_Query query

里面有一点变化
$query = new WP_Query( array( 'post_type' => 'photo', 'posts_per_page'=>56, 'paged'=> $paged, 'catphoto' => $term->name, 'mark' =>$mark) );

敬祝Igor