php wordpress is_home() || is_index() 可能吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6700001/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 01:02:13  来源:igfitidea点击:

wordpress is_home() || is_index() possible?

phpwordpress

提问by Stefan

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

我在我的header.php 中有一个测试,看看我们是否在家显示英雄。

<?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?

但是当用户登陆 index.php 时,英雄没有被显示。显然没有 is_index() 条件,有谁知道我如何测试它的家或索引?

回答by brenjt

Try is_front_page()

尝试 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.

如果您位于站点的绝对根目录,那应该返回 true。

回答by Shef

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 ifstatement:

如果它仍然不起作用,请尝试在if语句之前添加以下内容:

<?php wp_reset_query(); ?>

回答by shelhamer

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

is_front_page()Wordpress 条件标签列表中试用。

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

当您在 wordpress 安装的“首页”上时​​确实如此,即:

  • 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
  • 如果您已将首页设置为博客/帖子,则为帖子页面
  • 您指定为首页的页面,如果您使用的是页面而不是您的帖子