首页 文章

Headers 内的Wordpress Headers

提问于
浏览
0

我想改变 Headers 在Wordpress上的header.php文件中的工作方式 . 目前我拥有它所以它将显示用户所在的每个页面的名称,除了加载博客帖子的页面之外,它的效果很好 .

我喜欢它设置所以header.php文件中的“title”元素也可以处理自定义名称,而不是在访问博客页面时加载最新博客的名称作为 Headers 我想要它只是说博客 .

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

的index.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,是否找不到变量?

2 回答

  • 2

    这样做的一种正确方法是使用the_title过滤器,您不需要修改模板,例如:

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

    如果你真的想使用你自己的var,你应该读这个:How to use own php variables in wordpress template?

  • 0

    更换

    the_title();
    

    echo get_the_title()
    

    希望这对你有用

相关问题