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

custom taxonomy - List taxonomies by post id

programmeradmin8浏览0评论

To start with I'm pretty new with php so any nice kind person who agrees to help me, please be gentle.

What I want is generate a list of taxonomy terms that are associated with a specific post id. I want to list them on a page using a shortcode. I pulled the code from the loop which does almost the same thing, but apparently it doesn't work outside of the loop and any attempts to adapt it have only succeed in generating errors. Google this far has not been particularly helpful. I'm sure the solution is extremely simple, but it escapes me. Any input would be appreciated.


<?php if(has_term('', 'themes')): ?>
    <span class="title">Themes</span>
    <p><?php echo get_the_term_list( $post->id, 'themes', ' ', ', ') ?></p>
<?php endif; ?> 

To start with I'm pretty new with php so any nice kind person who agrees to help me, please be gentle.

What I want is generate a list of taxonomy terms that are associated with a specific post id. I want to list them on a page using a shortcode. I pulled the code from the loop which does almost the same thing, but apparently it doesn't work outside of the loop and any attempts to adapt it have only succeed in generating errors. Google this far has not been particularly helpful. I'm sure the solution is extremely simple, but it escapes me. Any input would be appreciated.


<?php if(has_term('', 'themes')): ?>
    <span class="title">Themes</span>
    <p><?php echo get_the_term_list( $post->id, 'themes', ' ', ', ') ?></p>
<?php endif; ?> 
Share Improve this question asked Apr 22, 2020 at 0:49 DanielDaniel 1 4
  • If you're using it outside the loop, then what post's terms should it be displaying? – Jacob Peattie Commented Apr 22, 2020 at 1:19
  • what is the error you have seen? – 西門 正 Code Guy - JingCodeGuy Commented Apr 22, 2020 at 2:06
  • @simmongcc "I just get a generic "There has been a critical error on your website. Learn more about debugging in WordPress." – Daniel Commented Apr 22, 2020 at 2:20
  • @Jacob Peattie, I'm trying to figure out the proper way to show them by postid . It's for a fiction site, each story has a number of taxonomy terms associated with it and I'm trying to make it so the terms display by post id, instead of manually change them anytime a term is added. – Daniel Commented Apr 22, 2020 at 2:20
Add a comment  | 

1 Answer 1

Reset to default 0

Have you set WP_DEBUG and WP_DEBUG_LOG to true in your wp-config.php?

Doing so makes debugging a lot easier as you'll get an error_log in your wp-content directory, which will tell you what is it it your code that is causing the error.

If I had to make a guess, I'd say it's the $post->id that might be causing the problem as $post might be undefined. Unless you have defined it in your code earlier, but just left it out from the code you've shared. You could try replacing it with get_the_ID() as it defaults to the current global post like has_term does when no post or post_id is provided.

But check your error_log to be sure.

发布评论

评论列表(0)

  1. 暂无评论