gpt4 book ai didi

wordpress - WooCommerce - 显示特定作者的产品文件信息

转载 作者:行者123 更新时间:2023-12-04 07:38:44 24 4
gpt4 key购买 nike

我想在产品存档页面(商店列表)上的购物车前回显一些文本,但仅在某个作者的产品上。
我有这个代码,它显示了我想要的信息:

add_filter( 'woocommerce_loop_add_to_cart_link', 'tomich_add_melody_vendor', 10, 3 );
function tomich_add_melody_vendor ( $add_to_cart_html, $product, $args )
{
$before = 'Shop: <img class="vendor-image-tomich" src="https://melodypayne.com/wp-content/uploads/2019/06/cropped-Melody-Payne-Store-Logo-Pic-2019.png" alt="Melody Logo"><span class="tomich-shop-title"><a href="/shop/melodypayne">Melody Payne</a></span></br></br>'; // Some text or HTML here
$after = ''; // Add some text or HTML here as well


return $before . $add_to_cart_html . $after;
}
但现在我需要弄清楚如何限制它只显示在某个作者的产品上,无论是通过昵称还是 ID。
我尝试了这段代码的一些变体,但没有一个奏效。
$author_id = get_post_field('post_author', get_queried_object_id());

if (get_the_author_meta('display_name', $author_id) === 'vivian') {
// do something
}

最佳答案

你路过 $product到您的自定义回调函数,因此您可以使用它来获取作者信息,如下所示:

add_filter('woocommerce_loop_add_to_cart_link', 'tomich_add_melody_vendor', 10, 3);

function tomich_add_melody_vendor($add_to_cart_html, $product, $args)
{

$author_nickname = get_the_author_meta('nickname', $product->post->post_author);

// OR
// $author_id = get_the_author_meta('ID', $product->post->post_author);

// OR
// $author_name = get_the_author_meta('display_name', $product->post->post_author);

// You could also get the author with other paramaeters like:
// display_name
// first_name
// ID
// last_name
// nickname

if ($author_nickname == "vivian") { // pass the name you want

$before = 'my custom element before'; // Some text or HTML here

$after = 'my custom element after'; // Add some text or HTML here as well


return $before . $add_to_cart_html . $after;

} else {

return $add_to_cart_html;
}

}

关于wordpress - WooCommerce - 显示特定作者的产品文件信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67605848/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com