检查当前页面是否为博客页面
9 个回答
- 投票数
-
- 2014-08-03
如果通过" 博客页面"表示您是在阅读设置中设置为帖子页面的静态页面,那么您可以通过执行以下操作进行检查:
if ( is_front_page() && is_home() ) { // Default homepage } elseif ( is_front_page() ) { // static homepage } elseif ( is_home() ) { // blog page } else { //everyting else }
当使用
is_home()
和is_front_page()
时,必须在 正确的顺序,以避免错误并测试每个用户配置.(来源: 条件标签-博客页面 )
或者简单地:
if ( !is_front_page() && is_home() ) { // blog page }
或更简单地说(我想):
if ( is_home() ) { // blog page }
If by 'blog page' you meant a static page set as posts page in the Reading Settings, then you could check it by doing this:
if ( is_front_page() && is_home() ) { // Default homepage } elseif ( is_front_page() ) { // static homepage } elseif ( is_home() ) { // blog page } else { //everyting else }
When you use
is_home()
andis_front_page()
, you have to use them in the right order to avoid bugs and to test every user configuration.(Source: Conditional Tags - The Blog Page)
Or simply:
if ( !is_front_page() && is_home() ) { // blog page }
Or more simply (I suppose):
if ( is_home() ) { // blog page }
-
至少在我看来,使用`if(!is_front_page()&&is_home())`的一种用例是,如果您要为**默认首页**和**blog分配具有不同布局样式的主题页**.One use case to use `if ( !is_front_page() && is_home() )`, at least in my opinion, is if you are distributing a theme that has different layout style for the **default homepage** and the **blog page**.
- 0
- 2014-08-03
- Giraldi
-
我发现无论是否选择博客存档或页面,is_front_page()都将返回true.需要验证.https://codex.wordpress.org/Function_Reference/is_front_pageI'm finding is_front_page() will return true whether or not the blog archive or a page is selected. Need verification. https://codex.wordpress.org/Function_Reference/is_front_page
- 0
- 2017-05-28
- atwellpub
-
- 2014-04-18
您可以在主题functions.php文件中使用以下内容:
function is_blog () { return ( is_archive() || is_author() || is_category() || is_home() || is_single() || is_tag()) && 'post' == get_post_type(); }
然后将其放入要检查的文件中
<?php if (is_blog()) { echo 'You are on a blog page'; } ?>
您可以在functions.php文件中使用Hooks来挂钩上述内容,以使其出现在每个页面上.
You can use the following in your themes functions.php file:
function is_blog () { return ( is_archive() || is_author() || is_category() || is_home() || is_single() || is_tag()) && 'post' == get_post_type(); }
And then put this in the file you are checking:
<?php if (is_blog()) { echo 'You are on a blog page'; } ?>
You can use Hooks in your functions.php file to hook the above, to make that appear on every page.
-
如果您想确定自己是否在_a_博客页面上,而不必在_the_博客页面上(如博客主页中一样),则这是一个很好的答案.参见@Giraldi的答案.This is a great answer if you want to determine if you're on _a_ blog page, but not neccessarily _the_ blog page (as in the blog home page). See @Giraldi's answer for that.
- 1
- 2016-04-17
- Tim Malone
-
我错误地认为is_blog()存在,因为is_page()存在.我没有想到要查阅[官方WordPress条件标签索引](https://codex.wordpress.org/Conditional_Tags#Conditional_Tags_Index).我能够使用Widget Logic插件有效地应用此解决方案.I incorrectly assumed is_blog() exists because is_page() exists. It didn't occur to me to consult the [official WordPress Conditional Tags Index](https://codex.wordpress.org/Conditional_Tags#Conditional_Tags_Index). I was able to effectively apply this solution using the Widget Logic plugin.
- 0
- 2019-04-14
- Clarus Dignus
-
- 2016-04-17
如果"博客页面"是指在阅读中将静态页面设置为帖子页面:
global $wp_query; if ( isset( $wp_query ) && (bool) $wp_query->is_posts_page ) { //static blog page }
PS.此解决方案还适用于template_redirect 操作
If by 'blog page' you meant a static page set as posts page in the Reading:
global $wp_query; if ( isset( $wp_query ) && (bool) $wp_query->is_posts_page ) { //static blog page }
PS. This solution also works on template_redirect action
-
嗨,repinsa,欢迎来到WPSE :)感谢您添加答案.它被否决了,可能是因为它在代码中存在语法错误(在`global $ wp_query`之后遗漏了分号),还因为它没有完全回答问题.这是一个函数,但是OP在他的头文件中询问如何解决这个问题-因此可能需要更多关于放置位置的解释.再次欢迎您,很高兴有您在这里!Hi repinsa, welcome to WPSE :) Thanks for adding your answer. It's been voted down a bit, probably because it has a syntax error in the code (it's missing a semicolon after the `global $wp_query`) but also because it doesn't fully answer the question. It's a function, but the OP asked how to work this out in his header file - so it might need a little more explanation about what to put where. Again, welcome, glad to have you here!
- 0
- 2016-04-17
- Tim Malone
-
实际上,这是唯一的好答案,应该有更多的支持.That's actually the only good answer here, should have had more upvotes.
- 3
- 2017-12-23
- Lacho Tomov
-
- 2018-05-16
要获取博客索引页面,我发现
if ( !is_front_page() && is_home() ) { // blog page }
对我不起作用,我不得不使用get_option('page_for_posts')函数来标识Blog页面post_id,我的答案是
if ( !is_front_page() && is_home() ){ if ( empty ( $post_id) ) { global $post; $post_id = get_option( 'page_for_posts' ); } //blog page }
To get the blog index page, I found that
if ( !is_front_page() && is_home() ) { // blog page }
is not working for me, I had to use the get_option('page_for_posts') function to identify the Blog Page post_id, my answer is
if ( !is_front_page() && is_home() ){ if ( empty ( $post_id) ) { global $post; $post_id = get_option( 'page_for_posts' ); } //blog page }
-
- 2013-07-19
您可以使用..
<?php if ( is_single() ) { ?> Do stuff here <?php } ?>
检查它是否是一篇博客文章.或者...
<?php if ( is_home() ) { ?> Do stuff here <?php } ?>
检查是否是博客主页
You can use..
<?php if ( is_single() ) { ?> Do stuff here <?php } ?>
to check if it's a single blog post. Or...
<?php if ( is_home() ) { ?> Do stuff here <?php } ?>
to check if it's the blog homepage
-
如果您更改了博客页面,则无法使用Doesn't work if you've changed the blog page
- 2
- 2014-10-09
- cdmckay
-
这不能为OP提供正确的答案.这表明您在单个帖子上,而不是"博客页面".This doesn't provide a correct answer to the OP. This indicates you are on a single post, not "the blog page".
- 0
- 2017-12-27
- butlerblog
-
- 2016-10-04
有一个棘手的方法.
假设您的博客页面标签为
blog
,则可以使用此代码.global $wp_query; if($wp_query->query['pagename']=='blog'){ // this is blog page }
There is a tricky method.
Suppose if your blog page slug is
blog
, you can use this code.global $wp_query; if($wp_query->query['pagename']=='blog'){ // this is blog page }
-
- 2016-12-17
首页
if(is_home() && is_front_page() || is_front_page()): // static or default hompage ... endif;
博客
if(is_home() && !is_front_page()): // blog ... endif;
HOMEPAGE
if(is_home() && is_front_page() || is_front_page()): // static or default hompage ... endif;
BLOG
if(is_home() && !is_front_page()): // blog ... endif;
-
- 2017-09-16
我想我处于相同的情况下非常简单,我使用了以下技术来使用页塞.
if( is_page('blog') ) { echo "This is your blog page"; }
但是请确保您没有选择主页来显示最近的博客文章,并且已为博客或新闻等博客设置了特定页面,只需使用该页面标签,就可以了.
I guess its very simple I was in a same situation and I used the following technique which is to use the page slug.
if( is_page('blog') ) { echo "This is your blog page"; }
But make sure you've not selected homepage to display recent blog posts and you have set a specific page for blogs like blog or news etc, just use that page slug and you'd be fine.
-
- 2015-09-27
我用这种方式
// Get body classes as array $body_classes = get_body_class(); // Check if "blog" class exists in the array if(in_array("blog", $body_classes)) { // Do stuff }
I use this way
// Get body classes as array $body_classes = get_body_class(); // Check if "blog" class exists in the array if(in_array("blog", $body_classes)) { // Do stuff }
我是WordPress的新手.我正在寻找一种检查头文件代码中当前页面是否为博客页面的方法.
我已经检查过,但找不到方法.救救我,请.