0

我很想改变标题在 Wordpress 上 header.php 文件中的工作方式。目前我有它,所以它会显示用户所在的每个页面的名称,这很好用,除了加载博客文章的页面。

我希望它设置为 header.php 文件中的“标题”元素也可以处理自定义名称,即当您访问博客页面时,而不是加载最新博客的名称作为标题我希望它只是说博客.

我决定创建自己的变量,然后使用头检查来查看是否已设置,但我无法让它工作,将它放在调用头文件之前或之后。

索引.php

$header_title = "Blog";
get_header(); ?>

然后在 header.php 文件中,我有以下代码。

<title>
April Kelley | 
    <?php if (isset($header_title)) { 
        echo $header_title;
     } else {
        the_title(); 
    }?>

现在在我看来,这应该可行,那么我哪里出错了?

PS。我只打算在某些页面上使用这个 $header_title 变量,比如 index.php 和 seach.php 页面本身拉入博客文章,所以 isset 仍然会返回 false 是找不到变量吗?

4

2 回答 2

2

A proper way of doing this is to use the_title filter, you don't need to modify template, e.g. :

function my_title($title, $id) {
    if (is_home())
        $title.= ' | Blog';

    return $title;
}
add_filter('the_title', 'my_title');

If you really want to use your own var, you should read this : How to use own php variables in wordpress template?

于 2013-02-07T15:09:01.077 回答
0

代替

the_title(); 

作为

echo get_the_title()

希望这对你有用

于 2013-02-07T12:59:17.297 回答