2017-06-13 110 views

回答

0

用于创建过滤器类型的小部件。这必须按照帖子类型获取标签列表。

为此,我创建了一个通用函数,您可以根据帖子类型列出标签或任何分类列表。

功能get_terms_by_post_type($ taxo_term,$ post_types){

global $wpdb; 
$query = $wpdb->prepare(
          "SELECT term.*, COUNT(*) from $wpdb->terms AS term 
          INNER JOIN $wpdb->term_taxonomy AS texo ON term.term_id = texo.term_id 
          INNER JOIN $wpdb->term_relationships AS tr ON tr.term_taxonomy_id = texo.term_taxonomy_id 
          INNER JOIN $wpdb->posts AS post ON post.ID = tr.object_id 
          WHERE post.post_type IN('%s') AND texo.taxonomy IN('%s') 
          GROUP BY term.term_id", 
          join("', '", $post_types), 
          join("', '", $taxo_term) 
         ); 

$results = $wpdb->get_results($query); 
return $results; 

}

调用这个函数,并通过分类项和岗位类型。它会给你一个标签数组。

3
<!-- begin custom related loop, isa --> 

<?php 

// get the custom post type's taxonomy terms 

$custom_taxterms = wp_get_object_terms($post->ID, 'your_taxonomy', array('fields' => 'ids')); 
// arguments 
$args = array(
'post_type' => 'your_custom_post_type', 
'post_status' => 'publish', 
'posts_per_page' => 3, // you may edit this number 
'orderby' => 'rand', 
'tax_query' => array(
    array(
     'taxonomy' => 'your_taxonomy', 
     'field' => 'id', 
     'terms' => $custom_taxterms 
    ) 
), 
'post__not_in' => array ($post->ID), 
); 
$related_items = new WP_Query($args); 
// loop over query 
if ($related_items->have_posts()) : 
echo '<ul>'; 
while ($related_items->have_posts()) : $related_items->the_post(); 
?> 
    <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li> 
<?php 
endwhile; 
echo '</ul>'; 
endif; 
// Reset Post Data 
wp_reset_postdata(); 
?> 
+0

我的问题是针对特定的帖子类型。不是一个单一的职位。所以这对我的情况没有用 –

+0

如果您在wp_get_object_terms($ post_ids,'post_tag',$ optional_args)中将ur post类型的值作为post _tag值传递;你会得到结果。请参考链接https://codex.wordpress.org/Function_Reference/wp_get_object_terms – priya

+0

@priya你应该正确地阅读文档。你给出的链接清楚地提到'post_tag'的论点必须是一个分类。你不能在那里传递文章类型。 –

相关问题