【问题标题】:Change default page title wordpress using filter使用过滤器更改默认页面标题 wordpress
【发布时间】:2020-09-05 08:22:01
【问题描述】:

我正在尝试更改 WordPress 中的默认博客文章标题(Hello world)

add_filter('the_title', 'modify_all_titles', 10, 2);

function modify_all_titles($title, $id) {

return 'My default page title';
}

如您所见,它会更改所有博客文章的标题。

如何仅更改默认页面标题(例如第一篇文章或文章 ID1)?

提前谢谢你。

【问题讨论】:

    标签: wordpress filter page-title


    【解决方案1】:

    你应该使用 wordpress conditional tags

    在您的情况下,is_single() 是适当的功能。

    检查当前查询是否为 id 为 1 的帖子:is_single(1)

    与帖子的 slug 相同:is_single('hello-world')

    带有条件标签的代码:

    add_filter('the_title', 'modify_all_titles', 10, 2);
    
    function modify_all_titles($title, $id) {
    
    return $id == 1 ? 'My default page title' : $title;
    }
    

    您还可以将$id 与 hello world 的 id 进行比较

    【讨论】:

    • 谢谢。不幸的是这个: add_filter('the_title', 'modify_all_titles', 10, 2);函数 modify_all_titles($title, $id) { return is_single(1) ? '我的默认页面标题' : $title; } 不会改变帖子标题
    • 是的,现在很好。非常感谢您,我非常感谢您的帮助。谢谢。
    • ywc,,, 乐于助人
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-22
    • 1970-01-01
    • 1970-01-01
    • 2015-09-29
    相关资源
    最近更新 更多