douke9379 2016-09-18 10:06
浏览 153

如何获取自定义分类项的术语(post_tag)? WordPress的

I created custom post type 'photo' and custom taxonomy 'catphoto'.

The type 'photo' supports 'catphoto' and 'post_tag' as taxonomies.

Now I should make one filter for a client.

I need to show on taxonomy-catphoto.php page all post_tags, which belong to the current 'catphoto' item.

For example, I have a post of custom post type 'photo'. The name of this post is 'Plane'. This post belongs to '1961-1981' catphoto item. Also it has post tags like 'space', 'planes', 'stars', 'war'.

Also, for example, I have a post of 'photo' which name is 'Soldier'. It belongs to '1941-1961' catphoto item and has 'WW2', 'USA', 'USSR' like post tags.

And now when I select 1941-1961 catphoto item I should get a list with: WW2 USA USSR

I try like this:

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/>';
        }
    }
}

Now I get all post tags of all catphoto items. How can I fix to restrict for definite catphoto item. For example, '1941-1961' ?

  • 写回答

1条回答 默认 最新

  • 普通网友 2016-09-18 16:26
    关注

    I have solved this via $_GET query. So easy... For beginning, I had decided to create own taxonomy type (like post_tags). Its name is 'mark'.

    and after that the snippet is:

    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>';
    }
    

    So ugly code here and there but it works.

    and now in taxonomy-catphoto.php I fill in:

    $mark = $_GET['mark'];
    

    and after that a little change inside WP_Query query

    $query = new WP_Query( array( 'post_type' => 'photo', 'posts_per_page'=>56, 'paged'=> $paged, 'catphoto' => $term->name, 'mark' =>$mark) );
    

    Best regards, Igor

    评论

报告相同问题?

悬赏问题

  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教