PHP-主页和内部页面上的不同标题文本


PHP - different header text on homepage and interior pages

我需要使主页页眉中的页面标题与内部页面的标题不同。这看起来像是一个简单的php修复,但它超出了我的想象。这似乎是解决方案,但实现它时出现了错误代码:PHP如果URL等于This,则执行操作

这是我的代码:

<?php
/**
 * The Header for our theme.
 */
?><!DOCTYPE html>
<html>
<head>
    <link href='http://fonts.googleapis.com/css?family=GFS+Didot'
          rel='stylesheet' type='text/css'>
    <title><?php wp_title('|', true, 'right'); ?></title>
    <?php wp_head(); ?>
</head>
<body <?php body_class(); ?>>
<div>
    <header class="pagetitle">
        <h1>
            <a href="<?php echo esc_url(home_url('/')); ?>">
                <?php bloginfo('name'); ?>
            </a>
        </h1>
        <nav>
            <?php wp_nav_menu(array('theme_location' => 'primary')); ?>
        </nav>
    </header>

有人能告诉我if/else声明吗?它会在主页上给我一个,在所有其他页面上给我另一个?

试试这个:

<title>
    <?php 
        if( is_home() ) {
            echo 'Homepage Title';
        } 
        else {
            echo 'Other Pages Title';
        }
    ?>
</title>

参考:http://codex.wordpress.org/Function_Reference/is_home

以下是我在自己的网站上使用的内容,只在主页上显示网站标题,但在子页面上显示文章标题:

<title>
    <?php wp_title(''); ?><?php if(wp_title('', false)) { echo ' |'; } ?> <?php bloginfo('name'); ?>
</title>

参考:http://codex.wordpress.org/Function_Reference/wp_title

if( is_home() || is_front_page() ) {
    echo 'HOMEPAGE';
} 
else {
    echo 'INTERIOR PAGES';
}

您提到的问题的最新编辑显示了正确的if/else语句:

$host = $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
if($host == 'your.homepage.com/url/etc') {
  echo "Homepage header text";
}
else {
  echo "Other header text";
}

检查用户是否在主页的wordpress特定解决方案是函数is_home(),如果显示博客主页,它将返回布尔值。有关is_home()函数的更多详细信息,请参阅此处。wordpress中还有其他"条件标签",它们都有自己的功能。可以在这里找到这些列表。

例如,您还可以使用is_home()函数重写上面的PHP代码块:

if (is_home()) {
  echo "Homepage header text";
}
else {
  echo "Other header text";
}