Php WooCommerce:覆盖“我的帐户”页面中“浏览器”选项卡的标题集

Php WooCommerce:覆盖“我的帐户”页面中“浏览器”选项卡的标题集,php,wordpress,woocommerce,Php,Wordpress,Woocommerce,当访问WooCommerce中的My Account页面时,我正在尝试更改浏览器选项卡中设置的标题 例如,当我转到订单页面时,该选项卡仍然命名为我的帐户,这不太好。它应该始终具有端点的名称/帐户菜单项。我在这里尝试过,但这只会更改左上角菜单内容上的标题: /** * Change title for menu items */ add_filter( 'the_title', 'custom_account_endpoint_titles' ); function custom_accoun

当访问WooCommerce中的
My Account
页面时,我正在尝试更改浏览器选项卡中设置的标题

例如,当我转到
订单
页面时,该选项卡仍然命名为
我的帐户
,这不太好。它应该始终具有端点的名称/
帐户菜单项
。我在这里尝试过,但这只会更改左上角菜单内容上的标题:

/**
 * Change title for menu items
 */
add_filter( 'the_title', 'custom_account_endpoint_titles' );
function custom_account_endpoint_titles( $title ) {
    global $wp_query;

    if ( in_the_loop() && isset( $wp_query->query_vars['orders'] ) ) {
        return 'Orders';
    }

    return $title;
}
截图:


尝试改用
pre\u get\u document\u title
过滤器,因为这样可以在呈现之前对其进行修改

注意,
$title\u片段
实际上是一个数组,看起来像

array (
  'title' => 'title example',
  'tagline' => 'just another wordpress blog'
}
所以你需要这样做

add_filter( 'pre_get_document_title', 'custom_account_endpoint_titles' );

function custom_account_endpoint_titles($title_pieces) {

    global $wp_query;

    if ( in_the_loop() && isset( $wp_query->query_vars['orders'] ) ) {

      $title_pieces['title'] = 'Orders';
      //$title_pieces['tagline'] = 'Your tag line'; Set a tag line if you want to

      return $title_pieces;
    }

    return $title_pieces;

}

另外,请确保转储
$wp\u query->query\u vars['orders']
的值,以确保它是您实际查找的值

我终于找到了过滤器以及更改它的方法:

/**
 * Override woocommerce account endpoint titles in the browser tab
 */
add_filter( 'wpseo_title', 'woocommerce_endpoint_titles' );
function woocommerce_endpoint_titles( $title ) {
    $sep       = ' – ';
    $sitetitle = get_bloginfo();

    if ( is_wc_endpoint_url( 'orders' ) ) {
        $title = 'Bestellungen' . $sep . $sitetitle;
    } elseif ( is_wc_endpoint_url( 'view-order' ) ) {
        $title = 'Bestellung' . $sep . $sitetitle;
    }

    return $title;
}
如果您在WooCommerce中创建了自己的端点,您也可以使用此筛选器,但需要先注册自定义端点。您可以这样做:

/**
 * Add custom menu items to wc query vars so that we can use is_wc_endpoint_url()
 */
add_filter( 'woocommerce_get_query_vars', 'add_items_to_query_vars' );
function add_items_to_query_vars( $vars ) {

    foreach ( [ 'custom-endpoint-1', 'custom-endpoint-2' ] as $e ) {
        $vars[ $e ] = $e;
    }

    return $vars;
}

我希望这对某人有所帮助。

您使用的是哪个WordPress主题?@Mr.Me我正在使用Divided,但它不起作用。仍在选项卡中显示我的帐户:(删除
global$wp_query;
,删除
if
语句并设置
$title='test'
。然后呢?确保清除缓存以及您使用的是什么版本的wp?使用安装了Divi主题和Yoast SEO的版本4.9.9。检查
head.php
模板文件以查看是否使用
wp_head()