What I would like to achieve is getting the posts categories and subcategories (without needing subcategories to be related to this post) (when viewing the post) in a list outside of the loop, preferably in a sidebar widget. I want it to get updated automatically when displaying different posts without needing the user to specify the categories or posts manually from back-end. I tried with GLOBALS but couldn't get it to work.
Anyone done anything like this before? How should I approach?
Thanks in advance.
What I would like to achieve is getting the posts categories and subcategories (without needing subcategories to be related to this post) (when viewing the post) in a list outside of the loop, preferably in a sidebar widget. I want it to get updated automatically when displaying different posts without needing the user to specify the categories or posts manually from back-end. I tried with GLOBALS but couldn't get it to work.
Anyone done anything like this before? How should I approach?
Thanks in advance.
Share Improve this question edited Mar 9, 2020 at 17:05 Arg Geo asked Mar 9, 2020 at 13:41 Arg GeoArg Geo 4591 gold badge8 silver badges20 bronze badges 4 |1 Answer
Reset to default 0You can use this in your sidebar.php. I highly recommend you to use a child theme.
Here is a minimum example of such a tag list. you can also make a widget out of it. Learn here.
And add html based on your theme's design structure.
<ul>
<?php
$terms = wp_get_post_terms( get_queried_object_id(), 'taxonomy-name-here' );
foreach($terms as $term){
?>
<li>
<a href="<?php echo get_term_link( $term ); ?>"><?php echo $term->name; ?></a>
</li>
<?php } ?>
</ul>
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744683955a4587800.html
get_queried_object_id()
should return the id, Just tested with my stock _s theme.var_dump(get_queried_object_id());
returningint(9511)
. Are you sure your sidebar or something else is not modifying the query? Can you trywp_reset_postdata()
before your code? – Sahriar Saikat Commented Mar 9, 2020 at 16:59