我的帐户模板中的 Woocommerce 当前端点页面标题

Woocommerce current endpoint page titles in my account template

我正在使用 woo-commerce 用户仪表板模板。

我需要打印当前端点的标题而不是 the_title();

以下是

的图像快照

DOMIAN.com/my-account/orders/.页面标题应该是 "My Order" 但它是 "My Account".

其他端点标题也有相同要求。

请帮帮我。

我的帐户菜单项的原始顺序可以在 /wp-content/plugins/woocommerce/includes/wc-account-functions 中看到。php

/**
 * Get My Account menu items.
 *
 * @since 2.6.0
 * @return array
 */
function wc_get_account_menu_items() {
 return apply_filters( 'woocommerce_account_menu_items', array(
 'dashboard' => __( 'Dashboard', 'woocommerce' ),
 'orders' => __( 'Orders', 'woocommerce' ),
 'downloads' => __( 'Downloads', 'woocommerce' ),
 'edit-address' => __( 'Addresses', 'woocommerce' ),
 'payment-methods' => __( 'Payment Methods', 'woocommerce' ),
 'edit-account' => __( 'Account Details', 'woocommerce' ),
 'customer-logout' => __( 'Logout', 'woocommerce' ),
 ) );
}

您可以使用 woocommerce_account_menu_items 过滤器更改这些端点的顺序,您也可以使用相同的过滤器更改列表项标题。

<?php
function wpb_woo_my_account_order() {
    $myorder = array(
        'my-custom-endpoint' => __( 'My Stuff', 'woocommerce' ),
        'edit-account'       => __( 'Change My Details', 'woocommerce' ),
        'dashboard'          => __( 'Dashboard', 'woocommerce' ),
        'orders'             => __( 'Orders', 'woocommerce' ),
        'downloads'          => __( 'Download MP4s', 'woocommerce' ),
        'edit-address'       => __( 'Addresses', 'woocommerce' ),
        'payment-methods'    => __( 'Payment Methods', 'woocommerce' ),
        'customer-logout'    => __( 'Logout', 'woocommerce' ),
    );
    return $myorder;
}
add_filter ( 'woocommerce_account_menu_items', 'wpb_woo_my_account_order' );

更改列表项标题的一个限制是它不会更改 entry-title。

更改 WooCommerce 自定义端点的 entry-title 的一种方法是使用具有 in_the_loop 条件的 the_title 过滤器。

<?php
/*
 * Change the entry title of the endpoints that appear in My Account Page - WooCommerce 2.6
 * Using the_title filter
 */
function wpb_woo_endpoint_title( $title, $id ) {
    if ( is_wc_endpoint_url( 'downloads' ) && in_the_loop() ) { // add your endpoint urls
        $title = "Download MP3s"; // change your entry-title
    }
    elseif ( is_wc_endpoint_url( 'orders' ) && in_the_loop() ) {
        $title = "My Orders";
    }
    elseif ( is_wc_endpoint_url( 'edit-account' ) && in_the_loop() ) {
        $title = "Change My Details";
    }
    return $title;
}
add_filter( 'the_title', 'wpb_woo_endpoint_title', 10, 2 );

显示端点标题

<?php
add_filter( 'the_title', 'wc_page_endpoint_title' );
the_title( '<h1 class="page-title mb-4">', '</h1>' );
?>

wc_page_endpoint_title

奖金 - 端点文档标题

/**
 * Generates account page titles
 *
 * @see wc_page_endpoint_title
 *
 * @param string $title Current title.
 * @return string
 */
function vralle_generate_wc_endpoint_title( string $title ): string {
    $action   = isset( $_GET['action'] ) ? sanitize_text_field( wp_unslash( $_GET['action'] ) ) : '';
    $endpoint = WC()->query->get_current_endpoint();
    $ep_title = $endpoint ? WC()->query->get_endpoint_title( $endpoint, $action ) : '';
    if ( ! empty( $ep_title ) ) {
        return $ep_title;
    }

    return $title;
}
/**
 * Filters document title.
 *
 * @param array $title The document title parts.
 *
 * @return array
 */
function vralle_document_title( $title ) {
    if ( is_page() && function_exists( 'is_wc_endpoint_url' ) && is_wc_endpoint_url() ) {
    $title['title'] = vralle_generate_wc_endpoint_title( $title['title'] );
    }
    return $title;
}
add_filter( 'document_title_parts', 'vralle_document_title );