在类别页面前端显示产品类别自定义字段的内容

时间:2020-05-08 13:05:00

标签: php wordpress woocommerce custom-fields

我在这里使用了代码:Adding custom field to product category,将自定义字段添加到woocommerce类别页面。

使用的代码包括检索数据的用法:

echo $productCatMetaTitle = get_term_meta($term_id, 'wh_meta_title', true);
echo $productCatMetaDesc = get_term_meta($term_id, 'wh_meta_desc', true);

现在,我不确定如何将每个类别的“ wh_meta_desc”的内容拖入每个相关类别页面前端的“ woocommerce_before_main_content”挂钩空间中?

例如此处:https://themirrorman.uk/category/wall-mirrors/

如果我在PRODUCT页面中存储了一个名为“ wh_meta_title”的自定义字段,我将具有等效的代码来实现此目的。它将获取“ wh_meta_desc”的内容,并将其放入其产品页面上的“ woocommerce_before_main_content”挂钩空间:

function add_before_main_content() {
   global $post;
   $product_id = $post->ID;

   $productCatMetaTitle = get_post_meta($product_id,'wh_meta_title',true);
   if(!$productCatMetaTitle) return;
   echo ''.$productCatMetaTitle.'';
}
add_action('woocommerce_before_main_content','add_before_main_content');

但是如何修改此代码,以使其从其新创建的CATEGORY页面自定义字段中提取“ wh_meta_title”?然后在woocommerce_before_main_content钩子空间的前端显示其内容?

我的代码看起来像这样吗?

/** WooCommerce product category custom field - woocommerce_before_main_content - content **/
function add_before_main_content() {
global $post; //Is '$post' correct to apply this function to product category pages?
   $term_id = $term->term_id;
   $productCatMetaTitle = get_term_meta($term_id, 'wh_meta_title', true);
   if(!$productCatMetaTitle) return;
   echo ''.$productCatMetaTitle.'';
}
}
add_action('woocommerce_before_main_content','add_before_main_content');

谢谢。

更新: 我现在在这里找到了合适的解决方案:http://www.wpmusketeer.com/add-a-wysiwyg-field-to-woocommerce-product-category-page/

请客: adding text or html onto woocommerce category pages hookable spaces

或访问以查看其实际效果:https://themirrorman.uk/category/wall-mirrors/

0 个答案:

没有答案