• ronaldb73

    (@ronaldb73)


    I’m trying to make a special sidebar that only shows in one special category. I’ve done it in the theme Magazine Basic, but can’t get it to work in VN News.

    This is what I’ve done so far:
    1- in functions.php I’ve registered another sidebar (succesfully)
    2- I’ve made a new sidebar-test.php template (succesfully).

    In Design – Widgets I can select the new sidebar and add widgets to it.

    What didn’t work so far:
    In category.php I’ve tried, using the else if code, calling the new sidebar, but only in catogory with id ’62’. This works, only problem is all categories get to see the sidebar AND u see both sidebars (so the one specially for ’62’ and the normal sidebar, for all the other categories).

    This is the code I’m using, please tell me what I’m doing wrong:

    <?php
    if ( function_exists(‘dynamic_sidebar’) && dynamic_sidebar(2)) {
    include (TEMPLATEPATH . ‘/sidebar-2.php’);
    }
    elseif ( function_exists(‘dynamic_sidebar’) && dynamic_sidebar(2) && (is_category(’62’))) {
    include(TEMPLATEPATH . ‘/sidebar-test.php’);
    }

    ?>

Viewing 15 replies - 1 through 15 (of 15 total)
  • stvwlf

    (@stvwlf)

    Hi

    what you’re doing wrong is the elseif line is never executed.

    the first if tests for 2 conditions. the second elseif tests for 3 conditions. anything that would pass the second elseif’s test has already passed the first if’s test before it reaches the second if. Thus that code is not ever being reached

    this is more what you want

    if ( function_exists('dynamic_sidebar') && dynamic_sidebar(2)) {
       if (is_category('62'))) {
          include(TEMPLATEPATH . '/sidebar-test.php');
       } else {
          include (TEMPLATEPATH . '/sidebar-2.php');
       }
    }
    Doodlebee

    (@doodlebee)

    Yes, your code is off.

    if ( function_exists('dynamic_sidebar')

    basically says “If the “dynamic sidebar” function exists

    && dynamic_sidebar()) {

    and a dynamic sidebar is set, then display whatever you see here

    }

    if you want to give a specific sidebar name, then you put it here:

    && dynamic_sidebar('name')) {

    So what you’re trying to do with the code is way off.

    However, I’m not exactly clear on what it is you *are* trying to do. I’m sorry, but your explanation isn’t that clear (at least not to me). But from what I can *guess*, you want a sidebar that shows only specific things – have a widgetized sidebar that is displayed only for category 62?

    in that case, you need to open up your functions.php file and look for the call for the widegtized sidebar. It should look like this:

    if ( function_exists('register_sidebar') ) {
      register_sidebar(array(
                  'name'=>'Sidebar',
                  'before_widget' => '<div id="%1$s" class="widget %2$s">',
                  'after_widget' => '</div>',
                  'before_title' => '<h3>',
                  'after_title' => '</h3>',
        ));
    }

    you want to add an additional widgetized section, so you add a new section to it:

    register_sidebar(array(
                  'name'=>'Category 62 Sidebar',
                  'before_widget' => '<div id="%1$s" class="widget %2$s">',
                  'after_widget' => '</div>',
                  'before_title' => '<h3>',
                  'after_title' => '</h3>',
        ));

    Now when you open up your widgets, you’ll see a new sidebar in the dropdown menu.

    to associate that menu with the category, you open up your sidebar.php file and put in a conditional:

    <?php if(is_category('62')) {
             dynamic_sidebar('Category 62 Sidebar');
           } else {
             dynamic_sidebar()'
    } ?>
    Thread Starter ronaldb73

    (@ronaldb73)

    Ok, as both solutions above don’t work, let met try to explain:

    I did add a new sidebar in functions.php
    I can add widgets to it through the WP dashboard.
    I only want this new sidebar to show with category 62 (and all the posts in it)

    The standard themecode for inserting the sidebars is:
    <?php if ( function_exists(‘dynamic_sidebar’) && dynamic_sidebar(2) ) : else : ?>
    <?php include (TEMPLATEPATH . ‘/sidebar-2.php’); ?>
    <?php endif; ?>

    Now what do I change to make the new sidebar (sidebar-test) to show ONLY with category 62?

    stvwlf

    (@stvwlf)

    Hi

    I am not an expert in this area, but have been researching it for you.

    1) You refer to registering a new sidebar in functions.php. You also refer to including a custom sidebar template. As far as I know, when you register a dynamic sidebar and assign a widget to it, the sidebar template is not used – the widgets take priority. Do your two goals contradict?

    2) I found very little about conditional display of dynamic widget-ready sidebars. I did find this code, about conditionally activating a sidebar only on the homepage.

    <?php if ( is_front_page() ) { ?>
      <?php if ( !function_exists('dynamic_sidebar') || !dynamic_sidebar(3) ) : endif; ?>
    <?php } ?>

    3) I assume that you are displaying ONLY posts from category 62 on this category page. Is that correct?

    If so, you could try this code to see if it works. Change sidebar(3) to whatever number you used for your new sidebar.

    <?php if ( is_category('62') ) { ?>
      <?php if ( !function_exists('dynamic_sidebar') || !dynamic_sidebar(3) ) :
        include(TEMPLATEPATH . '/sidebar-test.php');
      endif;
    } ?>

    This loads, when the cat page is loading cat 62, the test sidebar file when there are no widgets defined for that sidebar. Within cat 62, if widgets are defined for that sidebar, they run.

    I’d appreciate knowing if this works for you. It may need a little tweaking.

    Doodlebee

    (@doodlebee)

    The above code will display the “sidebar-test.php” file on category 62, that’s for sure. but is the difficuly you’re having that you only want Category 62 posts to show in the sidebar – and you’re getting everything?

    In that case, you’ll probably not want to widgetize that sidebar. I can’t remember if widgets allow you to *include* categories – I know you can exclude.

    But basiclly in that “sidebar-test.php” file, you need to put:

    wp_list_categories('child_of=62');

    And it’ll put out the child category list for cat 62.

    If you want a list of post titles, you’ll have ti supply your own query:

    <?php $cat = new WP_Query('cat=62');
    if($cat->have_posts()) : ?>
    <ul>
    <?php while($cat->have_posts()) :$cat->the_post(); ?>
    <li><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></li>
    <?php endwhile; ?>
    </ul>
    <?php endif; ?>
    Thread Starter ronaldb73

    (@ronaldb73)

    I’ll try it later, when there are not that many people on the site.
    But to clearify things, let me try to explain one more time:

    There are many categories on the site. With every post or page you see the regular sidebar (there are 2, the largest sidebar is nr.2).
    This is all fine, no problem so far.

    But… there is a special category (nr 62). When a visitor of the website asks for a summary of the posts in this category, OR whenever someone reads a post, assigned to cat.62, I want them not to see the regular sidebar, but the new sidebar, nr. 3.

    It has nothing to do with what is shown ON or INSIDE the 3rd sidebar.

    stvwlf

    (@stvwlf)

    It has nothing to do with what is shown ON or INSIDE the 3rd sidebar.

    In that case you may be able to apply similar logic to the regular sidebar in addition to the logic above – only display the regular sidebar when it is not category 62
    <?php if ( !is_category(’62’) ) { ?>
    <?php if ( !function_exists(‘dynamic_sidebar’) || !dynamic_sidebar(1) ) :
    include(TEMPLATEPATH . ‘/sidebar-1.php’);
    endif;
    } ?>

    I agree with Doodlebee that this would be much simpler if you were not using widgets as your sidebars.

    stvwlf

    (@stvwlf)

    plugin http://wordpress.org/extend/plugins/widget-logic/

    Widget Logic
    This plugin gives every widget an extra control field called “Widget logic” that lets you control when the widget appears.

    i.e. is_category(’62’)

    Thread Starter ronaldb73

    (@ronaldb73)

    I’m sorry but it’s just not working./ Forget about the widgets and the content that should be or should not be displayed in the new sidebar. All I want is to have a new sidebar show only with posts in category ’62’.

    When i use this code, as suggested by stvwlf:

    <?php if ( is_category(’62’) ) { ?>
    <?php if ( !function_exists(‘dynamic_sidebar’) || !dynamic_sidebar(3) ) :
    include(TEMPLATEPATH . ‘/sidebar-test.php’);
    endif;
    } ?>

    The following error shows:
    Parse error: syntax error, unexpected T_STRING

    Widgetlogig doesn’t help.

    stvwlf

    (@stvwlf)

    <?php if ( is_category(’62’) ) {
    include(TEMPLATEPATH . ‘/sidebar-test.php’);
    } ?>

    Thread Starter ronaldb73

    (@ronaldb73)

    OK, we’re getting there.
    It now shows the sidebar-test.php when you ask for cat 62.
    But…it also shows the regular sidebar.

    This is the code I’m using now:
    <?php if ( is_category(’62’) ) {
    include(TEMPLATEPATH . ‘/sidebar-test.php’);
    } ?>
    <?php if ( function_exists(‘dynamic_sidebar’) && dynamic_sidebar(2) ) : else : ?>
    <?php include (TEMPLATEPATH . ‘/sidebar-2.php’); ?>
    <?php endif; ?>

    Thread Starter ronaldb73

    (@ronaldb73)

    Fixed it by doing the following:

    <?php if ( is_category(’62’) ) {
    include(TEMPLATEPATH . ‘/sidebar-test.php’);
    }
    else if ( function_exists(‘dynamic_sidebar’) && dynamic_sidebar(2) ) : else : ?>
    <?php include (TEMPLATEPATH . ‘/sidebar-2.php’); ?>

    <?php endif; ?>

    Only bad thing is now I can’t use widgets in sidebar-test, or can I?

    stvwlf

    (@stvwlf)

    Whenever even one widget is active on a sidebar all code in sidebar files associated with that widget is skipped over.

    That is what this code means…

    <?php if ( !function_exists('dynamic_sidebar') || !dynamic_sidebar(1) ) :
          ... do things ...
    }

    “If a widgetized sidebar has not been defined in functions.php OR no widgets are loaded, then execute the code in the IF statement”. If either condition is true the sidebar file code is not executed. This is why we were saying last night it would be much easier without widgets.

    What widgets would you be using in the test sidebar? Usually the code that a widget replaces is pretty simple. The code to display the 5 most recent post titles is no more than ten lines long.

    Thread Starter ronaldb73

    (@ronaldb73)

    I’ve figured it out, as well as how to show the new sidebar with every single post in cat.62.
    I will be using the sidebar-test.php to show whatever I want to show in the sidebar with this category.

    poiuyt

    (@poiuyt)

    Ronaldb73 – I’ve read this forum with great interest, because I’m trying to do the same thing you’ve described. I want to have a unique sidebar show up on certain posts categorized together. I’m having the same problem you mentioned earlier that when I pull the new sidebar, the original sidebar shows up beneath it. What was your final solution to get the new sidebar show with every post in a specific category? Thanks!

Viewing 15 replies - 1 through 15 (of 15 total)
  • The topic ‘Problem with adding a sidebar’ is closed to new replies.