homepage - is_home() and is_front_page() not working in sidebar

admin2025-01-07  5

I'm trying to display a custom widget in the sidebar of my homepage (which is a static front page defined in Settings > Reading). Here is the relevant part of my sidebar.php:

if ( is_front_page() || is_home() ) {
    if ( is_active_sidebar( 'sidebar_lecture' ) ) : ?>
        <div id="secondary" class="widget-area" role="complementary">
            <?php dynamic_sidebar( 'sidebar_lecture' ); ?>
        </div>
    <?php 
endif;

The sidebar does not display because the conditional tag does not recognize my homepage as being a homepage.

When I print_r($wp_query) in the sidebar, it reveals that [is_page] => 1 and that the post's [ID] => 32, which is correct. However, [is_home] is null.

For the record, I did use a custom query (query_posts()) in my page.php template, but I did reset it afterwards using wp_reset_query().

Any pointers?

I'm trying to display a custom widget in the sidebar of my homepage (which is a static front page defined in Settings > Reading). Here is the relevant part of my sidebar.php:

if ( is_front_page() || is_home() ) {
    if ( is_active_sidebar( 'sidebar_lecture' ) ) : ?>
        <div id="secondary" class="widget-area" role="complementary">
            <?php dynamic_sidebar( 'sidebar_lecture' ); ?>
        </div>
    <?php 
endif;

The sidebar does not display because the conditional tag does not recognize my homepage as being a homepage.

When I print_r($wp_query) in the sidebar, it reveals that [is_page] => 1 and that the post's [ID] => 32, which is correct. However, [is_home] is null.

For the record, I did use a custom query (query_posts()) in my page.php template, but I did reset it afterwards using wp_reset_query().

Any pointers?

Share Improve this question asked Aug 24, 2013 at 3:48 JP LewJP Lew 3045 silver badges12 bronze badges 10
  • 2 is_home being null on a static front page is correct, is_front_page is the one you should be looking at. also, query_posts should never be used, use WP_Query instead and polluting the global query object will be a non-issue. – Milo Commented Aug 24, 2013 at 4:22
  • Put a exit( 'working' ); right after if ( is_front_page() || is_home() ) { to make sure the issue isn't with your sidebar...So do this if ( is_front_page() || is_home() ) { exit('working'); } to test... – user23654 Commented Aug 24, 2013 at 4:31
  • @Milo & @splashingpixels: thanks for the tips, problem solved. You are correct, is_front_page() is matching correctly. I was confused because when I print_r($wp_query), is_front_page does not even show up as a property, while all the rest of the conditional tags are there. If it's not in the WP_Query object, then where is the "is_front_page" flag set? – JP Lew Commented Aug 24, 2013 at 4:42
  • and if one of you wants credit for the answer, add an answer and I'll mark it. – JP Lew Commented Aug 24, 2013 at 4:43
  • @Milo: won't adding a new WP_Query object reduce the page-load speed? My assumption was that simply modifying the existing query would yield better performance than running a separate query. – JP Lew Commented Aug 24, 2013 at 4:46
 |  Show 5 more comments

2 Answers 2

Reset to default 0

I was having the same issue but found an anwser that worked for me. When you use wp_reset_query(); before you use is_home(); or is_front_page(); it will work just fine.

You can missing the semicolon at the end of the code. please replace the code with my code.

       if ( is_front_page() || is_home() ) {
           if ( is_active_sidebar( 'sidebar_lecture' ) ) : ?>
                <div id="secondary" class="widget-area" role="complementary">
                    <?php dynamic_sidebar( 'sidebar_lecture' ); ?>
                </div>
              <?php 
            endif;
        }

I hope it's work.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1736259303a579.html

最新回复(0)