Forums

[resolved] conditional IF ELSE statements not working (7 posts)

  1. jcprovideo
    Member
    Posted 3 years ago #

    I can't get my conditional statements to work correctly. Is there a better way of doing this?

    <div class="sidebar">
      <?php include (TEMPLATEPATH . "/searchform.php"); ?>
    
      <?php if ( is_page('homepage') ) { include (TEMPLATEPATH . '/homesidebar.php'); }
      		else {
    		include (TEMPLATEPATH . '/standardsidebar.php'); }
    		?>
    	<?php if ( is_page('about') ) { include (TEMPLATEPATH . '/aboutsidebar.php'); }
    	else {
    		include (TEMPLATEPATH . '/standardsidebar.php'); }
    		?>
            <?php if ( is_page('contact') ) { include (TEMPLATEPATH . '/contactsidebar.php'); }
    		else {
    		include (TEMPLATEPATH . '/standardsidebar.php'); }
    		?>
    </div>
    <!-- END SIDEBAR -->
  2. jcprovideo
    Member
    Posted 3 years ago #

    Basically what I'm trying to accomplish is display a generic sidebar for all other pages...i.e. search archives, tags, cats etc.

  3. moshu
    Member
    Posted 3 years ago #

    See some working examples how the
    if
    elseif
    else
    are combined: Conditional_Tags#Working_Examples

  4. jcprovideo
    Member
    Posted 3 years ago #

    Thanks that helped... I got it working. I had my PHP all wrong. Here is what I ended up with.

    <?php
    if (is_home()) { include (TEMPLATEPATH . '/regularsidebar.php'); }
    elseif (is_category()) { include (TEMPLATEPATH . '/regularsidebarsidebar.php'); }
    elseif (is_single()) { include (TEMPLATEPATH . '/regularsidebarsidebar.php'); }
    elseif (is_page()) {
    if (is_page('homepage')) { include (TEMPLATEPATH . '/homesidebar.php'); }
    elseif (is_page('contact')) { include (TEMPLATEPATH . '/contactsidebar.php'); }
    else { include (TEMPLATEPATH . '/regularsidebar.php'); }
    }
    else { include (TEMPLATEPATH . '/regularsidebar.php'); }
    ?>
  5. moshu
    Member
    Posted 3 years ago #

    Glad you got it working.
    (maybe you don't need the last "else" line twice...)

  6. jcprovideo
    Member
    Posted 3 years ago #

    thanks!

  7. cblock
    Member
    Posted 2 years ago #

    i am having a similar problem:
    My conditional statement works for all cases except arts-entertainment page. Below is my code, any help would be appreciated.

    ===================

    <?php
    if ( in_category('news') || post_is_in_descendant_category( 13 ) ) {
    include 'includes/nav-home.php';
    }
    elseif ( in_category('business') || post_is_in_descendant_category( 14 ) || is_page('business')) {
    include 'includes/nav-bus.php';
    }
    elseif ( in_category('arts-entertainment') || post_is_in_descendant_category( 17 ) || is_page('arts-entertainment')) {
    include 'includes/nav-ent.php';
    }
    elseif ( in_category('living') || post_is_in_descendant_category( 19 ) || is_page('living')) {
    include 'includes/nav-liv.php';
    }
    elseif ( in_category('opinion') || post_is_in_descendant_category( 18 ) || is_page('opinion')) {
    include 'includes/nav-opn.php';
    }
    elseif ( in_category('sports') || post_is_in_descendant_category( 20 ) || is_page('sports')) {
    include 'includes/nav-spt.php';
    }
    else {
    include 'includes/nav-home.php';
    }
    ?>

    ==================
    FYI: The idea is to use a different color for navigation for each page in the conditional statement.

Topic Closed

This topic has been closed to new replies.

About this Topic