【问题标题】:WP - How to change page title based on Page IDWP - 如何根据页面 ID 更改页面标题
【发布时间】:2021-01-16 19:50:44
【问题描述】:

我是 WP 新手,我想添加自定义函数来根据页面 ID 或 slug 更改页面的页面标题。?

我正在尝试在functions.php中使用它

全局 $wp_query;

             if (is_page('1987')){
              return $title;
                  }

              
              $title = 'Sale';
           
              return $title;          }

【问题讨论】:

标签: wordpress


【解决方案1】:

我终于解决了这个问题。

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 );

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-07-04
    • 2013-09-14
    • 1970-01-01
    • 1970-01-01
    • 2012-07-24
    • 1970-01-01
    • 2019-11-13
    • 1970-01-01
    相关资源
    最近更新 更多