Coder familiar with Wp? tell how to fix this please

by Tim3
4 replies
I have used the code below to set up dynamic sidebars for my Wp category pages, it works well but... the sidebars do not show up in the post pages in each category, grr
If someone could please correct the syntax so it will do that I will be eternally grateful.
Thanks.

<code>
$sidebar_id = ( is_category() ) ? sanitize_title( get_cat_name( get_query_var( 'cat' ) ) ) . '-sidebar' :
'sidebar';
dynamic_sidebar( $sidebar_id );
</code>
#coder #familiar #fix #wordpress
  • Profile picture of the author majick
    question is a little unclear sorry, it seems like you are saying you want the category sidebar to also show on single post displays that are in a category?

    if so you could try:
    ( is_category() || has_category() ) instead of just ( is_category() )

    just be aware you might have to avoid assigning multiple categories to a post if doing this, not sure what the effect of that would be, probably would give an error.
    {{ DiscussionBoard.errors[10408077].message }}
    • Profile picture of the author Tim3
      Originally Posted by majick View Post

      question is a little unclear sorry, it seems like you are saying you want the category sidebar to also show on single post displays that are in a category?

      if so you could try:
      ( is_category() || has_category() ) instead of just ( is_category() )

      just be aware you might have to avoid assigning multiple categories to a post if doing this, not sure what the effect of that would be, probably would give an error.
      Hi Majick,

      Thanks for repying,

      let me expand things a bit..

      The code gives each of the categories its own unique sidebar into which I can obviously insert a widget.

      I just need to make each category sidebar show up on each post within the category it is assigned to as per the example below.


      Category: Fruit (uses sidebar A)

      Posts:
      Post 1: /fruit/apples
      uses sidebar A

      Post 2: /fruit/oranges
      uses sidebar A

      Post 3; /fruit/grapes
      uses sidebar A

      --------

      Category: Vegetables (uses sidebar B)

      post 1: /vegetables/potatoes
      uses sidebar B


      I hope that makes things clearer.
      Signature

      {{ DiscussionBoard.errors[10408598].message }}
  • Profile picture of the author SteveJohnson
    What you're wanting to do is a little bit complicated.

    First, WP conditionals have unpredictable results when used outside of functions. So you should be enveloping your conditionals in a function, then call that function from the appropriate area in the theme file.

    I am making the assumption (from your description) that the desired sidebar corresponds to the top-level parent category.

    AND, I can see that they still haven't fixed the BB code tag.

    You can find the function in a pastebin here: http://pastebin.com/xy9fM4TD

    Code:
    function abc_show_sidebar() {
        // default value
         = 'sidebar';
         = false;
        
        // first check to see if this is a category page
        if ( is_cat() ) {
             = get_category( get_query_var( 'cat' ) );
        }
        // now check if this is a single post page
        elseif ( is_singular( 'post' ) ) {
            // grab the  object
             = get_post();
            // here is where you have to make the assumption that the post
            // has only one category assigned to it
            // get_the_category() returns an array of cat objects assigned to the post
             = get_the_category(  );
            // use only the first
             = ;
        }
        
        if (  ) {
            // this will go up through the category tree to find the top-level parent
            while ( 0 !=  ) {
                 = get_category(  );
            }
             =  . '-sidebar';
        }
        
        dynamic_sidebar(  );
        
    }
    Signature

    The 2nd Amendment, 1789 - The Original Homeland Security.

    Gun control means never having to say, "I missed you."

    {{ DiscussionBoard.errors[10410095].message }}
    • Profile picture of the author Tim3
      Originally Posted by SteveJohnson View Post

      What you're wanting to do is a little bit complicated.
      Hey Steve,

      Yeah I know, nothing I seem to do is ever simple, but being an awkward git keeps me going

      Thankyou so much for the code and your time in writing it out, I would never have figured it myself.

      Have a great weekend
      Signature

      {{ DiscussionBoard.errors[10411237].message }}

Trending Topics