2

can someone explain why the else statement is not working in WordPress for the sidebar.php?

<?php if(is_front_page() ) : ?>

 **content shows on main page sidebar**

<?php elseif(!is_front_page() ) : ?>
<?php // else: ?> // tried **else:** also

 **some content**
 **nothing is shown on any other page...**

<?php endif;?>
4
  • This is strange, this code should work: <?php if(is_front_page() || is_home()) { echo 'welcome home!'; } else { echo 'you are not home!'; } ?> Commented Jun 13, 2012 at 18:01
  • What is happening that leads you to believe it is not working? Commented Jun 13, 2012 at 18:04
  • @MrSlayer the content for the Front page is shown. but the else statement which should show content on all other pages is not processing. I stripped everything and just put Test Front and Test Others ... Test Others never appeared on any of the other pages Commented Jun 13, 2012 at 18:07
  • Have you tried the code using one of the default themes? I'm thinking there may be a call to query_posts in the theme or functions, without properly calling wp_reset_query() afterwards. Commented Jun 13, 2012 at 18:18

2 Answers 2

2

The is_front_page() conditional will always return false if it is used inside the loop or after the loop has ran (like in a sidebar). You can call wp_reset_query(); after your loop to reset the page query_vars.

See this answer on WPSE for more info.

Sign up to request clarification or add additional context in comments.

Comments

-1

Im not sure if is_front_page() exists, but just use {} around each condition result:

<?php if(is_front_page() ) { ?>

 **content shows on main page sidebar**

<?php } else { ?>
 // tried **else:** also

 **some content**
 **nothing is shown on any other page...**

<?php } ?>

2 Comments

according to WP is does codex.wordpress.org/Conditional_Tags#The_Front_Page ... is there a setting or feature that would turn off sidebar.php on all Pages or than the main? something is stopping the loading of the sidebar content on other pages.
This doesn't really do anything differently. PHP lets you use the if(): else: endif; syntax or if(){}else(){} and they are functionally equivalent.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.