WooCommerce 如何显示特定作者的产品存档信息
WooCommerce how to show Information on Product Archive IF by Certain Author
我想在产品存档页面(商店列表)上回显购物车前的一些文本,但仅限于特定作者的产品。
我有这段代码可以在我想要的地方显示我想要的信息:
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;
}
但现在我需要弄清楚如何限制它只显示在某个 author
的产品上,可以是 nickname
或 ID
。
我尝试了此代码的一些变体,但 none 有效。
$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 get_post()
function you can use another Wordpress function get_userdata()
检索 post 作者后得到用户的昵称和其他数据。
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 ) {
// Gets the "WP_Post" object of the product
$post = get_post( $product->get_id() );
// Gets the user ID
$user_id = $post->post_author;
// Gets "WP_User" object
$user = get_userdata( $user_id );
// Gets the user's data
$user_login = $user->user_login;
$user_nicename = $user->user_nicename;
$user_email = $user->user_email;
$display_name = $user->display_name;
// run your code here
}
代码已经过测试并且可以工作。将其添加到您的活动主题 functions.php.
我想在产品存档页面(商店列表)上回显购物车前的一些文本,但仅限于特定作者的产品。
我有这段代码可以在我想要的地方显示我想要的信息:
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;
}
但现在我需要弄清楚如何限制它只显示在某个 author
的产品上,可以是 nickname
或 ID
。
我尝试了此代码的一些变体,但 none 有效。
$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 get_post()
function you can use another Wordpress function get_userdata()
检索 post 作者后得到用户的昵称和其他数据。
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 ) {
// Gets the "WP_Post" object of the product
$post = get_post( $product->get_id() );
// Gets the user ID
$user_id = $post->post_author;
// Gets "WP_User" object
$user = get_userdata( $user_id );
// Gets the user's data
$user_login = $user->user_login;
$user_nicename = $user->user_nicename;
$user_email = $user->user_email;
$display_name = $user->display_name;
// run your code here
}
代码已经过测试并且可以工作。将其添加到您的活动主题 functions.php.