wordpress is_home() || is_index() possible?

2020-05-29 00:25发布

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?

标签: php wordpress
3条回答
家丑人穷心不美
2楼-- · 2020-05-29 00:28

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楼-- · 2020-05-29 00:33

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
查看更多
别忘想泡老子
4楼-- · 2020-05-29 00:35

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.

查看更多
登录 后发表回答