我是WP的新手,我想添加自定义功能,以根据Page ID或Slug更改页面的页面标题。?
我正在尝试在functions.php中使用它
全局$ wp_query;
if (is_page('1987')){ return $title; } $title = 'Sale'; return $title; }
答案 0 :(得分:0)
我终于用这个解决了。
function wpse309151_title_update( $title, $id = null ){
global $wp;
$current_slug = $wp->request;
// See if this post is a direct child of Products
if ($current_slug === 'page_slug_here') {
// If it is, override the title
$title = "SALE";
}
// Always return the title
return $title;
}
add_filter( 'the_title', 'wpse309151_title_update', 10, 2 );
function wpse309151_remove_title_filter_nav_menu( $nav_menu, $args ) {
// we are working with menu, so remove the title filter
remove_filter( 'the_title', 'wpse309151_title_update', 10, 2 );
return $nav_menu;
}
// this filter fires just before the nav menu item creation process
add_filter( 'pre_wp_nav_menu', 'wpse309151_remove_title_filter_nav_menu', 10, 2 );
function wpse309151_add_title_filter_non_menu( $items, $args ) {
// we are done working with menu, so add the title filter back
add_filter( 'the_title', 'wpse309151_title_update', 10, 2 );
return $items;
}
// this filter fires after nav menu item creation is done
add_filter( 'wp_nav_menu_items', 'wpse309151_add_title_filter_non_menu', 10, 2 );