javascript 在wordpress中检测主页有哪些不同的方法?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4920736/
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-10-25 15:21:55  来源:igfitidea点击:

What are the different ways to detect home page in wordpress?

phpjavascriptwordpress-themingwordpress

提问by Tushar Ahirrao

What are the different ways to detect wordpress homepage

检测wordpress主页的不同方法有哪些

except is_front_page()and is_home()

除了is_front_page()is_home()

Thanks

谢谢

回答by Mild Fuzz

is_front_page()is what you want.

is_front_page()是你想要的。

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()根据 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.

is_home()在您的主要博客页面上is_front_page()返回 true而在任何页面被定义为您的首页、提要与否时返回 true。

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.

此条件标签检查主页是帖子还是页面。这是一个布尔函数,这意味着它返回 TRUE 或 FALSE。当显示主博客页面并且设置->阅读->前页显示设置为“您的最新帖子”时,或设置为“静态页面”且“前页”值为正在显示的当前页面。

回答by Liam Galvin

I just do the following:

我只是执行以下操作:

if ( $_SERVER["REQUEST_URI"] == '/' ) { }

It works and doesn't overcomplicate things, especially as is_front_page()and is_home()don't always work as you'd expect them to.

它有效并且不会使事情变得过于复杂,尤其是因为is_front_page()并且is_home()并不总是像您期望的那样工作。

回答by TBar

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.

像魅力一样工作...... $_SERVER 是我一直使用的,它总是有效。

回答by Chris Chalmers

from outside the loop:

从循环外:

if(get_option("page_on_front") == $post->ID){
    //do front page stuff here
}

回答by Gediminas

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:

对我来说is_front_page()is_home()我需要检查主页的方式不起作用,所以我写了这个条件:

global $wp;  
$current_url = home_url(add_query_arg(array($_GET), $wp->request));
if ($current_url==get_site_url()) { 
    // code for homepage 
}

回答by Jim Camomile

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.

在许多情况下,Wordpress 站点的 is_home 和 is_frontpage 在 REAL 主页和主要博客页面上都可以 eval 为 true。在 Wordpress 中建立网站大约 4 年之后,这仍然困扰着我。

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.

例如,如果您有一个网站,您的主页上有最新帖子,其中可能包含滑块或其他一些以主页为中心的元素,并且还有另一个博客页面,那么 is_frontpage 和 is_home 在两个页面上都将评估为真。所以Wordpress对于真正的主页并没有明确的条件函数,至少大多数人对网站主页的看法是这样的。

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

所以我同意 Liam 的观点,如果你陷入混乱的境地,比如 if ( $_SERVER["REQUEST_URI"] == '/' ) { }

is more reliable.

更可靠。

回答by nino

is_home()is the way to go.

is_home()是要走的路。

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.

你试过这个方法还是不管用吗?如果是,它通常与 mod_rewrite 设置或 wordpress 设置本身有关。