• Resolved Wendihihihi

    (@wendihihihi)


    I’m a bit stuck. For my website I want a different sidebar on the homepage than on other pages.

    I have ried the script below, but it’s not working. The ‘endif’ kills the first ‘if’, but this one I need to continue for the script to work.

    How can I use ‘if’ twice and ‘endif’ whereby ‘endif’ doesn’t end the first ‘if’?

    Thanks

    <div id="mainright">
    <?php if ( is_home()) { ?>
    <div id="sidebar" class="clearblock">
    
    <ul>
    
    <li id="sidebartabs">
    <?php include (TEMPLATEPATH . "/sidebartabs.php"); ?>
    </li>
    <li>
    <?php include (TEMPLATEPATH . '/sponsors.php'); ?>
    </li>
    
    <?php if ( !function_exists('dynamic_sidebar') || !dynamic_sidebar('Right Sidebar') ) : ?>
    
    <?php endif; ?>
    
    </ul>
    </div>
    <?php } else { ?>
    <div id="sidebar" class="clearblock">
    
    <div class="feed">
    
    <h2 class="sh">Subscribe here </h2>
    
    <?php $feed = get_option('sdf_feed')?>
    
    <form action="https://feedburner.google.com/fb/a/mailverify" method="post" target="popupwindow" onsubmit="window.open('https://feedburner.google.com/fb/a/mailverify?uri=<?php echo($feed); ?>', 'popupwindow', 'scrollbars=yes,width=550,height=520');return true">
    <input type="text" class="input" value="Sign Up here for email feed..." onfocus="if (this.value == 'Sign Up here for email feed...') {this.value = '';}" onblur="if (this.value == '') {this.value = 'Sign Up here for email feed...';}" name="email"/>
    <input type="hidden" value="<?php echo($feed); ?>" name="uri"/>
    <input type="submit" class="sbutton" value="Submit" />
    </form>
    
    </div>
    
    </div>
    <?php } ?>
    
    </div>
Viewing 8 replies - 1 through 8 (of 8 total)
  • Maybe this can clear things up a bit. The first ‘if ( is_home’ uses an opening brace ‘{‘, and is paired with ‘ } else {‘ and the closing brace ‘}’.

    The second ‘if ( !function_exists’ uses a colon, and so is paired with the ‘endif;’. This if statement is totally enclosed in the first part of the first if. Right now, it does nothing if the conditions are met.

    Opening braces match with closing braces. Colons match with endif;.

    <div id="mainright">
    <?php if ( is_home()) { ?>
       <div id="sidebar" class="clearblock">
       =========== Do some other stuff  ==========
       <?php if ( !function_exists('dynamic_sidebar') || !dynamic_sidebar('Right Sidebar') ) : ?>
          ========== Do nothing  ==========
       <?php endif; // End if ( !function_exists ?>
    
       </ul>
       </div>
    <?php } else { ?>
       <div id="sidebar" class="clearblock">
       =========== Do some other stuff  =======
    <?php } // End if ( is_home() ?>
    Thread Starter Wendihihihi

    (@wendihihihi)

    I don’t get it. Why does <?php } else { ?> not close the is_home?

    How must I change it in order to make it work?

    Sorry I wasn’t clear. It does close the is_home, as I said above. ‘is_home {‘ is paired with ‘} else {‘, and the else is paired with ‘}’. In your first post you said that the endif closed the first if, but it doesn’t, it closes the second one.

    It looks like the second if is not doing anything. There are no statements to execute when the test is true.

    I don’t understand what you want to do to ‘make it work’. Please explain.

    Paraphrased, your code looks like this:

    if is_home is true
          do a couple of includes
          if the dynamic sidebar function does not exist ...
              do nothing
          endif
    else = if is_home is false
          create a form
    } = end of if is_home()
    Thread Starter Wendihihihi

    (@wendihihihi)

    Ya, I think you are right; I didn’t make myself clear enough. This is exactly what I want:

    if is_home is true
    do a couple of includes
    AND include a dynamic sidebar for widgets

    else
    create another couple of includes
    AND include a dynamic sidebar for widgets

    I hope this is clearer.

    Thanks for your replies

    Thread Starter Wendihihihi

    (@wendihihihi)

    I started from scratch and created sidebar.php, sidebar-home.php and sidebar-other.php

    It almost works except is_home is not working. I have tested it with <?php $wp_query->is_home = true; ?> and it nicely shows up, on every page of course… This is not what I want.

    It somehow doesn’t get the message that it’s the homepage.

    Tried out is_front_page, but this is not what I want.
    All the plugins are deactivated.

    Any idea?

    These are the files.
    sidebar.php

    <?php
    if ( is_home() ) { ?>
    
    <?php include (TEMPLATEPATH . '/sidebar-home.php'); ?>
    
    <?php } else { ?>
    <?php include (TEMPLATEPATH . '/sidebar-other.php'); ?>
    
    <?php }

    sidebar-home.php

    <div id="mainright">
    
    			<div id="sidebar" class="clearblock">
    
    				<ul>
    					<li id="sidebartabs">
    						<?php include (TEMPLATEPATH . "/sidebartabs.php"); ?>
    					</li>
    
    <li>
    <?php include (TEMPLATEPATH . '/sponsors.php'); ?>
    </li>
    
    <?php if ( function_exists('dynamic_sidebar') || dynamic_sidebar('Home Sidebar') ) : ?>
    
    <?php endif; ?>	
    
    				</ul>
    			</div>
    
    		</div>

    sidebar-other.php

    <div id="mainright">
    
    			<div id="sidebar" class="clearblock">
    
    				<ul>
    					<li id="sidebartabs">
    						<?php include (TEMPLATEPATH . "/sidebartabs.php"); ?>
    					</li>
    
    <?php if ( !function_exists('dynamic_sidebar') || !dynamic_sidebar('Other Pages Sidebar') ) : ?>
    
    <?php endif; ?>	
    
    				</ul>
    			</div>
    
    		</div>

    The only possibility I can see is in the ‘if’ test in sidebar-home.php. I think you need to add !’ before function_exists, like this:

    <?php if ( !function_exists('dynamic_sidebar') || dynamic_sidebar('Home Sidebar') ) : ?>
    
    <?php endif; ?>

    Hi,
    i am used to going this kind of thing:diferent sidebars or headers if is home or page…
    i am not a king in php(learning) si i create a template for the home with get sidebar for example and if i want another sidebar in my page.php i include sidebar2.php on page.php.(you have to create sidebar2.php as well obviously)

    it works for me.it may be not what you re looking for…hope it can help

    Thread Starter Wendihihihi

    (@wendihihihi)

    Thanks for your reply.

    This morning I woke up fresh and added the $wp_query->is_home = true; ?>. Suddenly it worked. It is a bit strange though, because according to me every page should get the homepage sidebar now, but it’s not. Well, I’m not going to touch it anymore. It works!

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘‘endif’ for 2 ‘if’ functions but not wanting to end the first ‘if’’ is closed to new replies.