I've made an option of creating unlimited sidebars, then there is an option to select any desired sidebar from a select element, which is a custom meta field, in each category edit page. So far so good, everything works and every category displays the selected sidebar.

Now what i am trying to do is to output the same sidebar in every post (single.php) as in the category this post is assigned to.

Have anyone done something similar?

The code for outputting the sidebar in category page is:

if ( is_category() ){
   $sidebar = get_post_meta( $post->ID, 'select_a_sidebar', true );
   dynamic_sidebar( $sidebar );
  • 1
    Without knowing how your existing code works, how can we guess at how to extend it?
    – s_ha_dum
    May 25, 2014 at 13:28
  • Just added the code.
    – Vasili
    May 25, 2014 at 13:39

1 Answer 1


Retrieving category post is assigned to is rather straightforward with get_the_category(). However note that post can be easily placed into multiple categories and you will need some (quite elaborate) logic if you want to apply any particular sorting logic to it.

From looking at your code however it's not clear how exactly category/post/sidebar are tied to each other since you are retreiving sidebar info from post alone.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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