最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

taxonomy - Output terms for custom post types

programmeradmin4浏览0评论

I was able to echo out all terms belonging to a custom taxonomy using the code below:

$args = array('post_type' => 'my_post_type','number' => '999');
$terms = get_terms( 'my_taxo',
          [
            'hide_empty' => true,
            'orderby' => 'wpse_last_word',
          ] , $args );

foreach ( $terms as $term ) { 
    echo '' . $term->name . ''; 
}

This code provides the results I want on my localhost (xampp). Basically, it outputs all of the terms assigned to a particular post. But when I upload the code to a live server, the code no longer works as expected. Instead, it shows all the terms without filtering them. I even updates the PHP version on my live server match to the local server; still no luck.

Can anyone point out the issue on my code.

I was able to echo out all terms belonging to a custom taxonomy using the code below:

$args = array('post_type' => 'my_post_type','number' => '999');
$terms = get_terms( 'my_taxo',
          [
            'hide_empty' => true,
            'orderby' => 'wpse_last_word',
          ] , $args );

foreach ( $terms as $term ) { 
    echo '' . $term->name . ''; 
}

This code provides the results I want on my localhost (xampp). Basically, it outputs all of the terms assigned to a particular post. But when I upload the code to a live server, the code no longer works as expected. Instead, it shows all the terms without filtering them. I even updates the PHP version on my live server match to the local server; still no luck.

Can anyone point out the issue on my code.

Share Improve this question edited May 30, 2017 at 18:14 Dave Romsey 17.9k11 gold badges56 silver badges70 bronze badges asked May 30, 2017 at 13:15 Roshan DeshapriyaRoshan Deshapriya 1721 gold badge2 silver badges10 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 4

Your code is wrong. I don't know how it is working in your localhost. Cause-

  1. You are calling get_terms() with 3 parameters which actually accepts 2 parameters. The last one is extra.
  2. And secondly get_terms() returns all the terms of a taxonomy, not the terms associated with a post.

For getting the terms associated for a post you can use wp_get_post_terms.

Usage of wp_get_post_terms inside WordPress loop-

For each post you'll get the post's terms by calling wp_get_post_terms like below-

//Do something if a specific array value exists within a post
$term_list = wp_get_post_terms($post->ID, 'your_taxonomy', array("fields" => "all"));
// Then you can run a foreach loop to show the taxonomy terms infront.
foreach($term_list as $term_single) {
    echo $term_single->slug; //do something here
}

And for outside of the loop-

// Do something if a specific array value exists within a post
// And somehow you need to get the post ID to pas it to below.
$term_list = wp_get_post_terms($post_id, 'your_taxonomy', array("fields" => "all"));
// Then you can run a foreach loop to show the taxonomy terms infront.
foreach($term_list as $term_single) {
    echo $term_single->slug; //do something here
}

Hope that helps.

After few researchs I found below script do the work, thanks @the_dramatist pointing me to a right direction, I had use global $post, but not in a correct way

global $post;
$loop = new WP_Query(array('post_type' => 'myCPT', 'posts_per_page' => -1));
while ($loop->have_posts()) : $loop->the_post();
$terms = wp_get_post_terms($post->ID, 'my_taxonomy');                   
foreach($terms as $term_single) {
echo '' . $term_single->slug. '';
}
endwhile;

If anyone getting empty results after this, just delete the categories and re-create and apply them.

发布评论

评论列表(0)

  1. 暂无评论