简体   繁体   中英

Replace Woocommerce taxonomy archive pages title with ACF custom field

I am trying to figure out how to replace "woocommerce_page_title" with an ACF field? If there isn't one then it should fall back to "woocommerce_page_title();". I can't seem to find anything super helpful. Your help would be greatly appreciated.

add_filter( 'woocommerce_page_title', 'custom_title' );
function custom_title( $title ) {
    $title = get_field("custom_tag_h1");
    return $title;
}

For Archives pages taxonomy titles and ACF:

The woocommerce_page_title works for shop page and taxonomy archive pages, so in ACF:

在此处输入图片说明

Then in a product category for example (here "Clothing") , you set your custom title:

在此处输入图片说明

In the code, you need to get the queried object (the WP_Term object) for the taxonomy archive pages and to set it as following:

add_filter( 'woocommerce_page_title', 'custom_title' );
function custom_title( $page_title ) {
    if ( ! function_exists('get_field') || is_search() )
        return $page_title;

    if ( is_tax() ) {
        $term   = get_queried_object();
        $the_id = $term->taxonomy . '_' . $term->term_id;
    } elseif ( is_shop() ) {
        $the_id = wc_get_page_id( 'shop' );
    }
    return get_field( "custom_tag_h1", $the_id ) ? get_field( "custom_tag_h1", $the_id ) : $page_title;
}

Code goes in function.php file of your active child theme (or active theme). Tested and work.

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM