wordpress is_home() || is_index() possible?

2020-05-29 00:41发布

问题:

I have a test in my header.php to see whether we are at home to display a hero or not.

<?php if ( is_home() && have_posts() ) : ?>
  <div id="hero" class="inner clearfix">
    ...
  </div>
<?php endif ?>

But when the user lands on index.php the hero is not being shown. apparently there is no is_index() condition, does anyone know how I could test for if its home or index?

回答1:

Try is_front_page()

<?php if ( is_home() || is_front_page() ) : ?>
  <div id="hero" class="inner clearfix">
    ...
  </div>
<?php endif ?>

That should return true if you are on the absolute root of site.



回答2:

Try:

<?php if ( ( is_home() || is_front_page() ) && have_posts() ) : ?>
  <div id="hero" class="inner clearfix">
    ...
  </div>
<?php endif ?>

If it still doesn't work, try adding the following just before the if statement:

<?php wp_reset_query(); ?>


回答3:

Try out is_front_page() from the Wordpress conditional tags list.

It is true when you are on the "Front Page" of your wordpress installation, which is:

  • The posts page if you have set your front page to your blog/posts
  • The page you have designated as your front page, if you are using a page instead of your posts


标签: php wordpress