Current post categories and subcatecories outside of the loop

What I would like to achieve is getting the posts categories and subcategories (without needing subcategories to be rela

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
  • get_queried_object_id() should return the id, Just tested with my stock _s theme. var_dump(get_queried_object_id()); returning int(9511) . Are you sure your sidebar or something else is not modifying the query? Can you try wp_reset_postdata() before your code? – Sahriar Saikat Commented Mar 9, 2020 at 16:59
  • @SahriarSaikat thanks for your reply, I will remove the code - above code returns sub-categories only if they are associated with the post. Does nothing like what I want. I am at the beginning again. – Arg Geo Commented Mar 9, 2020 at 17:04
  • Dump get_queried_object_id() in sidebar and see what it returns – Sahriar Saikat Commented Mar 9, 2020 at 17:07
  • It returns the page id, correctly. – Arg Geo Commented Mar 9, 2020 at 17:14
Add a comment  | 

1 Answer 1

Reset to default 0

You 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

相关推荐

  • Current post categories and subcatecories outside of the loop

    What I would like to achieve is getting the posts categories and subcategories (without needing subcategories to be rela

    3天前
    70

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信