我在让分类学术语显示为我的产品文章片段的标题方面遇到了麻烦。这是一个使用 Avada 主题、Woocommerce 和高级自定义字段插件的 Wordpress 网站。分类法是由自定义分类法插件生成的,在网站的其他部分一切都很好。
到目前为止,我可以让所有的东西都显示在正确的地方,但我能做的最好的是 "呼应 "一个消息。在进一步阅读主题之前,我一直在获取分类法术语的ID号,并在ACF前端将 "对象ID "换成了 "术语名",但现在我无法获取ID和标题!我想请问一下,我是怎么做到的?
请大家帮忙,PHP不是我的强项,所以我相信这是新手的疏忽!
add_action( 'woocommerce_shop_loop_item_title', 'artist_link' );
function artist_link() {
$value = get_field("product_artist");
if($value)
{
echo "can't return value here, but I'd like the artist product term! ";
}
else
{
echo 'No Artist Entry';
}
}
这段代码来自我的function.php文件,因为Avada主题要求你在 "单-XXX.php "文件上使用循环。
我也应该提到这是一个多选的单选,"product_artist "来自ACF,自定义分类词ID是 "艺术家"。
谢谢!
*编辑:对于以后可能会遇到这个问题的人,我是这样做的。
add_action('woocommerce_shop_loop_item_title', 'artist_link');
function artist_link()
{
// Get the WooCommerce global variable for the current product
global $product;
if ($product instanceof \WC_Product) {
$value = get_field("product_artist", $product->get_id());
if ($value) {
echo $value->name;
} else {
echo 'Value falsey, do something with it here';
}
return;
}
echo 'No global product here';
}
看起来我试图调用一个字符串而不是一个对象(这就是$value)。
在看 调用 动作,有一个全局变量被带入范围,应该在调用动作之前设置,你应该可以使用这个变量。如果我没看错的话,这个变量应该是一个叫做 WC_Product
通过其父类有一个 get_id()
方法,我想这就是你需要传递给ACF的 get_field()
函数作为第二个参数。
add_action('woocommerce_shop_loop_item_title', 'artist_link');
function artist_link()
{
// Get the WooCommerce global variable for the current product
global $product;
if ($product instanceof \WC_Product) {
$value = get_field("product_artist", $product->get_id());
if ($value) {
echo $value;
} else {
echo 'Value falsey, do something with it here';
}
return;
}
echo 'No global product here';
}