What are the different ways to detect home page in

2020-05-29 01:26发布

What are the different ways to detect wordpress homepage

except is_front_page() and is_home()

Thanks

7条回答
爱情/是我丢掉的垃圾
2楼-- · 2020-05-29 02:02

For me is_front_page() and is_home() doesn't work in ways I need to check the homepage, so instead I write this condition:

global $wp;  
$current_url = home_url(add_query_arg(array($_GET), $wp->request));
if ($current_url==get_site_url()) { 
    // code for homepage 
}
查看更多
神经病院院长
3楼-- · 2020-05-29 02:06

With Twenty Ten I use:

<?php
 if ( $_SERVER["REQUEST_URI"] == '/' ) { ?>
   <h1 class="site-title"><?php bloginfo( 'name' ); ?></h1>
   <h2 class="site-description"><?php bloginfo( 'description' ); ?></h2>
<?php
} else { ?>
   <p class="site-title"><?php bloginfo( 'name' ); ?></p>
   <p class="site-description"><?php bloginfo( 'description' ); ?></p>
<?php } ?>

Works like a charm... $_SERVER is the one I always use and it always works.

查看更多
Fickle 薄情
4楼-- · 2020-05-29 02:16

is_home() is the way to go.

Have you tried this method and it doesn't work? If yes, it has usually something to do with the mod_rewrite settings or the wordpress settings itself.

查看更多
不美不萌又怎样
5楼-- · 2020-05-29 02:22

In many situations a Wordpress site can have is_home and is_frontpage both eval as true on the REAL homepage and also on the main blog page. After building sites in Wordpress for about 4 years this still bothers me.

For example if you have a site where you have your latest posts on your homepage with maybe a slider or some other homepage-centric elements, AND have another blog page, then is_frontpage and is_home will both eval as true on BOTH pages. So Wordpress does not have a clear conditional function for the true homepage, at least the way most people think of the homepage of a website.

So I agree with Liam that if you get into a confusing situation, something like if ( $_SERVER["REQUEST_URI"] == '/' ) { }

is more reliable.

查看更多
虎瘦雄心在
6楼-- · 2020-05-29 02:23

from outside the loop:

if(get_option("page_on_front") == $post->ID){
    //do front page stuff here
}
查看更多
小情绪 Triste *
7楼-- · 2020-05-29 02:24

is_front_page() is what you want.

I assume, by the fact that is_home() is not working, that your home page is static, according to the settings in wp-admin.

is_home() returns true on your main blog page whereas is_front_page() returns true on which ever page is defined as your front page, feed or not.

From codex:

This Conditional Tag checks if the main page is a posts or a Page. This is a boolean function, meaning it returns either TRUE or FALSE. It returns TRUE when the main blog page is being displayed and the Settings->Reading->Front page displays is set to "Your latest posts", or when is set to "A static page" and the "Front Page" value is the current Page being displayed.

查看更多
登录 后发表回答