2016-09-18 131 views
0

我创建了自定义帖子类型'照片'和自定义分类'catphoto'。如何获得自定义分类项目的术语(post_tag)? Wordpress

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

现在我应该为客户做一个过滤器。

我需要在上显示taxonomy-catphoto.php页面全部为post_tags,属于当前'catphoto'项目。

例如,我有一个自定义帖子类型'photo'的帖子。这篇文章的名字是'Plane'。这篇文章属于'1961-1981'catphoto项目。它也有帖子标签,如'空间','飞机','星号',''。另外,例如,我有'照片'这个名字是'Soldier'的帖子。它属于“1941年至1961年” catphoto项目和具有“二战”,“美国”,“苏联”杆状标签。

,现在当我选择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项目的所有帖子标签。我如何修复限制确定catphoto项目。例如,'1941-1961'?

回答

0

我已经通过$ _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>'; 
} 

因此,丑陋的代码在这里和那里,但它的工作原理。

现在在分类法catphoto.php我填写:

$mark = $_GET['mark']; 

,之后WP_Query查询

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

此致内变化不大,伊戈尔

相关问题