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

时间:2018-12-28 19:44:58

标签: php wordpress woocommerce

我目前正在尝试在WooCommerce中访问<link rel="icon" href="data:image/png;base64,..."> 页面时更改在浏览器选项卡中设置的标题。

例如,当我进入<link rel="icon" href="data:image/svg+xml,..."> 页面时,该选项卡仍被命名为My Account,但这并不是很好。它应该始终具有终结点的名称/ Orders。我在这里尝试过此操作,但这仅更改了左上方菜单内容上的标题:

My Account

屏幕截图:

enter image description here

2 个答案:

答案 0 :(得分:1)

请尝试使用pre_get_document_title过滤器,因为它使您可以在呈现之前对其进行修改。

请注意$title_pieces实际上是一个看起来像

的数组
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_query->query_vars['orders']的值,以确保其值为您实际要查找的值

答案 1 :(得分:1)

我终于找到了过滤器及其更改方法:

/**
 * 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;
}

我希望这对某人有帮助。