php - 将 Woocommerce 分类归档页面标题替换为 ACF 自定义字段

标签 php wordpress woocommerce advanced-custom-fields custom-taxonomy

我想弄清楚如何用 ACF 字段替换“woocommerce_page_title”?如果没有,那么它应该回退到“woocommerce_page_title();”。我似乎找不到任何 super 有用的东西。我们将非常感谢您的帮助。

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

最佳答案

对于文件页面分类标题和 ACF:

woocommerce_page_title 适用于商店页面和分类存档页面,因此在 ACF 中:

enter image description here

然后在产品类别中,例如(此处为“服装”),您可以设置自定义标题:

enter image description here

在代码中,您需要获取分类存档页面的查询对象(WP_Term对象)并将其设置如下:

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

代码位于事件子主题(或事件主题)的 function.php 文件中。经过测试并可以工作。

enter image description here

关于php - 将 Woocommerce 分类归档页面标题替换为 ACF 自定义字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53578989/

相关文章:

php - 要查找的正则表达式\"

php - 如何在mysql中显示多个表的列名

php - 从mysql中的多个表获取多个值?

javascript - 针对特定 JavaScript 忽略 Cloudflare Automatic RocketLoader

php - Woocommerce,根据运输类别隐藏运输方式

php - Symfony2 - 如何从我的网址中删除 'web/app_dev.php/'?

javascript - 将用户从现有网站重定向到现有 WP 博客的最佳方法是什么?

wordpress - 实现 SSL(Digital Ocean + Nginx + Cloudflare)

jquery - 在 WooCommerce 中使用 jQuery 获取可变产品价格?

php - 通过 Hook 在 WooCommerce 单个产品页面中显示自定义字段值