首页 > 解决方案 > 如何使用 Title() 过滤器挂钩来更改页面标题,而不是菜单标题

问题描述

我想使用过滤器挂钩在我的页面标题之前添加图像,但它也会将它们添加到我的菜单标题中。我怎样才能解决这个问题?

function scissorTitle ($title) 
{

 $scissortitle .= "<img src='*image_link*'>" . $title;



  return $scissortitle;
}
add_filter( 'title', 'scissorTitle', 10);

标签: phpwordpress

解决方案


过滤器title有两个参数$title$id您可以使用 $id 更改页面Check docs的唯一特定标题。您的完整代码应如下所示:

function scissorTitle ($title, $id) {
    global $post;

    if (is_singular() && $post->ID == $id) {
        $title = '<img src="image_link">' . $title;
    }

    return $title;
}

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

推荐阅读