• Resolved corfred

    (@corfred)


    Here is my goal:
    Specifically on a tag/archive page: I would like to show a different sidebar for each category that is displayed on the page.

    What I have done now: I have three categories, ‘Paintings’, ‘Photography’, and ‘Objects’. When a tag page is displayed, I want to check if it has any posts from the category ‘Paintings’ and if so, I would like it to display the dynamic_sidebar ‘paintings-sidebar’.. Photography -> photography_sidebar and Objects -> object_sidebar.

    This part I have accomplished.

    My issue comes when a tag/archive page contains posts from multiple categories. For example, posts from category ‘Paintings’ and posts from category’Photography’. In which case I would like to have ‘paintings_sidebar’ and ‘photography_sidebar’ show up.

    As it is now, only the sidebar for the most recent post’s category is displayed in the sidebar.

    What am I doing wrong? ??

    Here is my little bit of code:

    if (is_archive()) {
      if (in_category( 'paintings' )) {
        dynamic_sidebar( 'paintings-sidebar' );
      }
      if (in_category( 'photography' )) {
        dynamic_sidebar( 'photography-sidebar' );
      }
      if (in_category( 'objects' )) {
        dynamic_sidebar( 'objects-sidebar' );
      }
Viewing 11 replies - 1 through 11 (of 11 total)
  • Did you just not copy it, or are you actually missing the closing curly bracket at the end of your code snippet? It should look like this:

    if (is_archive()) {
      if (in_category( 'paintings' )) {
        dynamic_sidebar( 'paintings-sidebar' );
      }
      if (in_category( 'photography' )) {
        dynamic_sidebar( 'photography-sidebar' );
      }
      if (in_category( 'objects' )) {
        dynamic_sidebar( 'objects-sidebar' );
      }
    }

    The other thing that may be causing the problem is that you aren’t specifying which post the in_category function should be looking at, and by default, it looks at the current post, so you may need to set a boolean flag in the loop for each sidebar, then test the boolean flags in the area of the code where the sidebars are output to determine which to display.

    Thread Starter corfred

    (@corfred)

    Thanks for the response!
    I do have that closing curly bracket.
    And I was afraid that was going to be the solution as it is a bit above my knowledge level.
    It makes sense that it looks at the current post.

    I have been trying to find a way for it to scan all of the posts on that page and count if the any of those three categories has a count > 0, and if TRUE, add that sidebar.

    Does it sound like I am on the right track with that?

    Yes, I think you are on the right track. What you’d do is declare the boolean flags before the loop that outputs the content something like this:

    $has-paintings = false;
    $has-photographs = false;
    $has-objects = false;

    Then in the loop:

    if (in_category( 'paintings' )) {
        $has-paintings = true;
      }
      if (in_category( 'photography' )) {
        $has-photographs = true;
      }
      if (in_category( 'objects' )) {
        $has-objects = true;
      }

    Finally, in the section of the page that outputs the sidebar, you test for the flags:

    if ($has-paintings) {
      dynamic_sidebar( 'paintings-sidebar' );
    }
    if ($has-photographs) {
      dynamic_sidebar( 'photography-sidebar' );
    }
    if ($has-objects) {
      dynamic_sidebar( 'objects-sidebar' );
    }

    Thread Starter corfred

    (@corfred)

    So would this be right? or am I declaring the flags in the wrong place … because I am getting a server error everywhere that I attempt to place them on this page.

    By the way, I really appreciate your help.

    <section id="primary" class="site-content">
    		<div id="content" role="main">
    
    			<?php
    			$has-paintings = false;
    			$has-photographs = false;
    			$has-objects = false;
    			/* Start the Loop */
    			if ( have_posts() ) : while ( have_posts() ) : the_post();
    
    				 if (in_category( 'paintings' )) {
    				    $has-paintings = true;
    				  }
    				  if (in_category( 'photography' )) {
    				    $has-photographs = true;
    				  }
    				  if (in_category( 'objects' )) {
    				    $has-objects = true;
    				  } 
    
    				get_template_part( 'content', get_post_format() );
    
    			endwhile;
    
    			?>
    
    		<?php else : ?>
    			<?php get_template_part( 'content', 'none' ); ?>
    		<?php endif; ?>
    
    		</div><!-- #content -->
    	</section><!-- #primary -->

    I think that’s the right place, but you may need to use different syntax for the loop.

    What’s the server error you’re getting?

    Thread Starter corfred

    (@corfred)

    Still getting the error

    PHP Parse error: syntax error, unexpected ‘=’ in /wp-content/themes/twentytwelve/archive.php on line 26

    That is the line -> $has-paintings = false;

    I may have told you to do something really dumb.

    Try changing all the variables I gave you so they don’t include a “-” and see if that fixes it. For example, use $haspaintings instead of $has-paintings. Take the hyphen out of each of them. I think PHP is interpreting it as a minus sign.

    I’ve been doing so much CSS lately I wasn’t thinking PHP. Sorry about that.

    Thread Starter corfred

    (@corfred)

    Ha I will forgive you I guess.

    500 Error is gone! But, no sidebar is being displayed.

    From sidebar.php

    <div id="secondary" class="widget-area" role="complementary">
    				<?php
    				if (is_archive()) {
    				 	 if ($haspaintings) {
    					 	dynamic_sidebar( 'paintings-sidebar' );
    					 }
    					 if ($hasphotography) {
    						dynamic_sidebar( 'photography-sidebar' );
    					 }
    					 if ($hasobjects) {
    					 	dynamic_sidebar( 'objects-sidebar' );
    					 }
    				 } else {
    					dynamic_sidebar( 'sidebar-1' );
    				 }
    				 ?>
    		</div><!-- #secondary -->

    Forgiveness is good. : )

    Hmm… Maybe the problem now is that your theme is calling sidebar.php in a function called get_sidebar, which makes the variables you declared ($haspaintings, $hasphotography, and $hasobjects) not available in sidebar.php.

    In sidebar.php, try echoing the values of the variables you created in archive.php using the following code to see if the values are coming through:

    $myvars = array('haspaintings', 'hasphotography', 'hasobjects');
    foreach ($myvars as $val) {
     echo $val . ' = ' . $$val . '<br>';
    }

    The output will mess things up with the display of your theme, but it should tell us if the values are getting through or not.

    I’m going to have to go for the night, but if you find that the variables aren’t making it from archive.php to sidebar.php, try declaring the variables as “global” in sidebar.php like this:

    <?php
      global $haspaintings, $hasphotography, $hasobjects;
    				if (is_archive()) {
    				 	 if ($haspaintings) {
    					 	dynamic_sidebar( 'paintings-sidebar' );
    					 }
    					 if ($hasphotography) {
    						dynamic_sidebar( 'photography-sidebar' );
    					 }
    					 if ($hasobjects) {
    					 	dynamic_sidebar( 'objects-sidebar' );
    					 }
    				 } else {
    					dynamic_sidebar( 'sidebar-1' );
    				 }
    				 ?>

    to see if that will take care of it.

    Thread Starter corfred

    (@corfred)

    Oh golly!

    global $haspaintings, $hasphotography, $hasobjects;

    That did the trick! Thank you so so so much!

Viewing 11 replies - 1 through 11 (of 11 total)
  • The topic ‘conditional statement for multiple categories’ is closed to new replies.