簡體   English   中英

WooCommerce:覆蓋我帳戶頁面中“瀏覽器”選項卡上設置的標題

[英]WooCommerce: Override the title set to the browsers tab in my account page

我目前正在嘗試在WooCommerce中訪問“ My Account頁面時更改在瀏覽器選項卡中設置的標題。

例如,當我進入“ Orders頁面時,該選項卡仍被命名為“ My Account ,這並不是很好。 它應始終具有端點的名稱/ account_menu_items 我在這里嘗試過此操作,但這僅更改了左上角菜單內容上的標題:

/**
 * 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_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']的值,以確保其值為您實際要查找的值

我終於找到了過濾器及其更改方法:

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

我希望這可以幫助別人。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM