当前浏览:首页 / WordPress函数 / is_front_page()

is_front_page()

是否为网站的主页

is 条件判断more...

pagemore...


描述

取决于网站”阅读设置“的主页显示设置:“您的最新文章(show_on_front)”和“一个静态页面(page_on_front)”。

如果设置为静态页面,则在查看该页面时,此函数将返回true,否则与is_home()相同

更多类似的主题函数信息,请查看主题开发手册中的条件标签文章。


返回

(bool) “查询”(query)是否是针对网站的首页。



源码

查看源码 官方文档


更新日志

版本描述
2.5.0开始引入

使用示例

  • 示例1

    加载不同的header示例:

    if ( is_front_page() ) :
    	get_header( 'front' );
    else :
    	get_header();
    endif;
    
  • 示例2

    如果使用静态页面作为首页,这很有用:

    <title><?php bloginfo('name'); ?> &raquo; <?php is_front_page() ? bloginfo('description') : wp_title(''); ?></title>

    在自定义函数中的用法

    添加到主题functions.php文件中,此代码使用了is_front_page()条件,因此内容仅显示在首页上。

    add_action( 'loop_start', 'using_front_page_conditional_tag' );
    function using_front_page_conditional_tag() {
    	if ( is_front_page() ) {	
    		echo'<h2>Only Displays On The Front Page</h2>';
    	}
    }
    
  • 示例3

    要检查任意页面是否为主页(例如,当您处于自定义循环中时),可以使用此功能。它需要一个页面ID作为参数:

    function wpdocs_page_is_front_page( int $id ) {
        // 如果这里被设置为'page'以外的任何其他内容就是没有使用静态页面作为主页
        if ( 'page' !== get_option( 'show_on_front' ) ) {
            return false;
        }
    
        // option值是字符串,所以转为int
        $front_id = (int) get_option( 'page_on_front' );
    
        return $front_id == $id;
    }