我正在开发WordPress (woocommerce)
插件。
如果从特定产品的shop
链接设置了video
链接,如果没有设置video
链接,我想要显示相关的产品image
,那么我想要用video
替换产品图像(D3
页面),我怎么能这样做?
Code我正在使用
add_action( 'init', array($this, 'show_video_if_set_else_image') );
public function show_video_if_set_else_image(){
add_action('woocommerce_before_shop_loop_item_title', 'show_video');
function show_video(){
$linkk = get_post_meta(get_the_ID(), 'YT_video_link', true);
$linkk = substr($linkk, -11);
if($linkk != ''){
echo 'show video';
remove_action( 'woocommerce_before_shop_loop_item_title', 'woocommerce_template_loop_product_thumbnail', 10 );
?>
我也问了
这里
这个问题,请看一下。我对php中的OOP有点陌生,如果load是==
empty,那么在其他部分应该做些什么,因为对于某些产品,图像加载了twice (查看给定的链接),这对我来说有点出乎意料。
请帮忙,非常感谢。谢谢。
当我在remove_action( 'woocommerce_before_shop_loop_item_title', 'woocommerce_template_loop_product_thumbnail', 10 );部件中执行else时,它仍然加载第二个产品image,尽管为该产品设置了video,为什么会这样呢?
发布于 2020-04-23 19:36:25
它负载了两次,我相信它是由于另一个钩子也存在。您可能需要移除另一个不必要的钩子。实际上,您不必在另一个钩子中运行动作woocommerce_before_shop_loop_item_title
。
类删除操作的
如果您想在类中放置一个remove_action,可以使用不同的方法。下面的示例说明了两种方法。下面的类被证明适用于放置默认的商店主题的functions.php或插件,只要站点运行时不受其他插件的干扰。
class example {
public function __construct() {
// option1: place here will work
remove_action( 'woocommerce_before_shop_loop_item_title', 'woocommerce_template_loop_product_thumbnail', 10 );
// wrong demonstration: will not work because original call is not a class, it is not necessary to use class method to remove
// remove_action( 'woocommerce_before_shop_loop_item_title', array( $this 'woocommerce_template_loop_product_thumbnail' ), 10 );
add_action( 'init', array( $this, 'init' ) );
}
// option2: if you want to ***group different actions*** together, it also works.
public function init() {
// place here also work
// remove_action( 'woocommerce_before_shop_loop_item_title', 'woocommerce_template_loop_product_thumbnail', 10 );
// other remove actions or actions
}
}
new example();
如果您将动作移出init
钩子,然后重试并移除不需要的钩子。如果您想在没有init
挂钩的情况下将其添加到插件中,只需这样修改即可。
// your ' show_video' is already doing the job `show_video_if_set_else_image`
// so you might want to change this function name too
remove_action( 'woocommerce_before_shop_loop_item_title', 'woocommerce_template_loop_product_thumbnail', 10 );
add_action('woocommerce_before_shop_loop_item_title', array($this, 'show_video' ) );
function show_video(){
$linkk = get_post_meta(get_the_ID(), 'YT_video_link', true);
$linkk = substr($linkk, -11);
if($linkk != ''){
echo 'show video';
// if you put remove_action, it will not work as expected because the remove_action is run after add_action, so before it removes, it run already.
?>
关于优先级
如果你想改变标题挂钩的优先级,你只需要调整优先级,比如add_action( 'woocommerce_before_shop_loop_item_title', 'some_function', 50 );。
WordPress钩子系统是一个基于事件的系统。它遵循一系列称为筛选器或操作的事件。通常,过滤器允许您覆盖一个值,而操作允许您执行额外的作业。所以它是按照优先级和顺序运行的。对于行动来说,它会在特定的时间起作用。所以一次只做一份工作。除非有特殊的原因,否则大多数情况下都不需要使用其他作业来添加作业。因为action本身独立存在于
do_action()
定义的不同点。
即使添加了init钩子,woocommerce_before_shop_loop_item_title的队列仍然在do_action( 'woocommerce_before_shop_loop_item_title')运行,不会在更早的时候运行。更改钩子的优先级是同一个钩子,因为可能会有其他要重写的操作。然后,您可以调整钩子以便稍后运行,以确保您的重写工作。
关于double running
可能有一些现有的woocommerce_before_shop_loop_item_title操作在做类似的事情。因此,如果它输出两次,因为您只输出了一次,而原始动作输出了一次。
如果你想删除现有的行动(S)。只需运行:(以下是Woocommerce默认操作)
remove_action( 'woocommerce_before_shop_loop_item_title', 'woocommerce_template_loop_product_thumbnail', 10 );
您可以参考
remove_action()
钩子名称、函数名和优先级必须与原始名称相同,以便删除它,因此您可以参考该源代码或在文件夹中查找代码以获得准确的信息。
https://wordpress.stackexchange.com/questions/364912
复制相似问题