Woocommerce 5.4.1:如何将挂钩添加到存档页面上的特定 Post - 商店页面
Woocommerce 5.4.1: How to Add Hook to a specific Post on Archive Page - Shop Page
经过大量搜索仍未找到任何结果,我如何才能将此挂钩仅用于存档页面上的单个 Post?
目标是针对不同的Post使用不同的Text,但是在post这个位置。 例如。 10%、20%、30%
正如您在图片上看到的,该功能应用于每个 Post,这不是目标。
也许我必须使用 Post ID 到 select 并使用特定 Post 的功能。
// define the woocommerce_before_shop_loop_item_title callback
function action_woocommerce_before_shop_loop_item_title( ) {
echo "<div>
<small>Get a discount of 10% on any item</small>
</div>";
};
// add the action
add_action( 'woocommerce_before_shop_loop_item_title', 'action_woocommerce_before_shop_loop_item_title', 10, 0 );
上面脚本的结果:
是的,您可以使用产品 ID 将文本添加到特定 post
// define the woocommerce_before_shop_loop_item_title callback
function action_woocommerce_before_shop_loop_item_title( ) {
global $post;
if($post->ID == your_post_id){
echo "<div>
<small>Get a discount of 10% on any item</small>
</div>";}
};
// add the action
add_action( 'woocommerce_before_shop_loop_item_title', 'action_woocommerce_before_shop_loop_item_title', 10 );
经过大量搜索仍未找到任何结果,我如何才能将此挂钩仅用于存档页面上的单个 Post?
目标是针对不同的Post使用不同的Text,但是在post这个位置。 例如。 10%、20%、30%
正如您在图片上看到的,该功能应用于每个 Post,这不是目标。
也许我必须使用 Post ID 到 select 并使用特定 Post 的功能。
// define the woocommerce_before_shop_loop_item_title callback
function action_woocommerce_before_shop_loop_item_title( ) {
echo "<div>
<small>Get a discount of 10% on any item</small>
</div>";
};
// add the action
add_action( 'woocommerce_before_shop_loop_item_title', 'action_woocommerce_before_shop_loop_item_title', 10, 0 );
上面脚本的结果:
是的,您可以使用产品 ID 将文本添加到特定 post
// define the woocommerce_before_shop_loop_item_title callback
function action_woocommerce_before_shop_loop_item_title( ) {
global $post;
if($post->ID == your_post_id){
echo "<div>
<small>Get a discount of 10% on any item</small>
</div>";}
};
// add the action
add_action( 'woocommerce_before_shop_loop_item_title', 'action_woocommerce_before_shop_loop_item_title', 10 );