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; ?> 
  • If you're using it outside the loop, then what post's terms should it be displaying? – Jacob Peattie Apr 22 '20 at 1:19
  • what is the error you have seen? – simongcc Apr 22 '20 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 Apr 22 '20 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 Apr 22 '20 at 2:20
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.

  • I figured out what was causing the error and it's super simple. It was generated by the shorcode plugin I'm using. It requires you to remove the first and last php tags. Once I figured that out, I got the above code to work, now i just need to figure out how to change it so I can display it for a specific post id. – Daniel Apr 25 '20 at 5:46
  • Thank you all! It is very much appreciated! – Daniel Apr 25 '20 at 15:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.