wordpress WooCommerce - 我在哪里可以编辑钩子生成的 HTML?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14357381/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
WooCommerce - where can I edit HTML generated by hooks?
提问by Michael Giovanni Pumo
I'm new to WooCommerce. Anyhow, I want to create my own theme, so I followed the guidelines and copied accross the core template files to /mywordpresstheme/woocommerce/.
我是 WooCommerce 的新手。无论如何,我想创建自己的主题,所以我遵循了指南并将核心模板文件复制到/mywordpresstheme/woocommerce/。
That all works great and I'm editing the templates just fine.
这一切都很好,我正在编辑模板。
However, the way hooks and actions work in WooCommerce is baffling me and I can't work out where certain parts of generated HTML are coming from.
然而,WooCommerce 中钩子和动作的工作方式让我感到困惑,我无法弄清楚生成的 HTML 的某些部分来自哪里。
For example, in content-product.php
, there is a hook that gets the image:
例如,在 中content-product.php
,有一个获取图像的钩子:
<?php
/*
* woocommerce_before_shop_loop_item_title hook
*
* @hooked woocommerce_show_product_loop_sale_flash - 10
* @hooked woocommerce_template_loop_product_thumbnail - 10
*/
do_action( 'woocommerce_before_shop_loop_item_title' );
?>
But what is this? Where does it come from?? Is there any clue in the action name as to where I could locate the HTML being generated for the purpose of editing it?
但这是什么?它从何而来??操作名称中是否有关于我可以在哪里找到为编辑而生成的 HTML 的任何线索?
I've read the article on 'hooks and filters' on WooCommerce, but it explains nothing regarding where or how to change these on a case for case basis.
我已经阅读了 WooCommerce 上关于“钩子和过滤器”的文章,但它没有解释关于在何处或如何根据具体情况更改这些内容。
Any help would be greatly appreciated.
任何帮助将不胜感激。
I'm new to this system and I'm sure I'm simply over-looking something very obvious.
我是这个系统的新手,我敢肯定我只是忽略了一些非常明显的东西。
Thanks, Mikey.
谢谢,米奇。
回答by helgatheviking
But what is this? Where does it come from?? Is there any clue in the action name as to where I could locate the HTML being generated for the purpose of editing it?
但这是什么?它从何而来??操作名称中是否有关于我可以在哪里找到为编辑而生成的 HTML 的任何线索?
This is an action hook
. It isn't doing anything by itself per say, but the functions listed in the comments hook
into it and therefore run when this function is triggered. It says in the comments that function woocommerce_template_loop_product_thumbnail
is the function responsible for getting the thumbnail. You can find this function inside the Woocommerce plugin. I use the Sublime Text editor (though I think others will do this too) to search the whole folder for that phrase and it tells me exactly what file it is in. In this case it is what is called a pluggable function and is located in woocommerce-template.php
. (It's now called wc-template-hooks.php in version 2.1+)
这是一个action hook
. 它本身并没有做任何事情,而是在hook
它的注释中列出的功能,因此在触发此功能时运行。它在评论中说函数woocommerce_template_loop_product_thumbnail
是负责获取缩略图的函数。您可以在 Woocommerce 插件中找到此功能。我使用 Sublime Text 编辑器(尽管我认为其他人也会这样做)在整个文件夹中搜索该短语,它会准确地告诉我它在哪个文件中。在这种情况下,它被称为可插拔函数,位于woocommerce-template.php
. (现在在 2.1+ 版本中称为 wc-template-hooks.php)
A pluggable function means that you define a new version of the function with the same name in your theme's functions.php
可插入函数意味着您在主题的functions.php 中定义了具有相同名称的新版本的函数
function woocommerce_template_loop_product_thumbnail(){
echo "apple";
}
If you put the above in your functions.php then instead of Woo's woocommerce_template_loop_product_thumbnail()
you'd merely see the word apple.
如果你把上面的内容放在你的functions.php 中,那么woocommerce_template_loop_product_thumbnail()
你只会看到apple 这个词,而不是Woo 。
I've read the article on 'hooks and filters' on WooCommerce, but it explains nothing regarding where or how to change these on a case for case basis.
我已经阅读了 WooCommerce 上关于“钩子和过滤器”的文章,但它没有解释关于在何处或如何根据具体情况更改这些内容。
You will make all changes in your theme's functions.php and a case by case basis isn't necessary. All hooks and filters behave the same. That said, they aren't the easiest thing to learn so have patience with yourself. I found filters to be especially tough to wrap my head around.
您将在主题的functions.php 中进行所有更改,并且不需要逐个案例。所有钩子和过滤器的行为都相同。也就是说,它们不是最容易学习的东西,所以对自己要有耐心。我发现过滤器特别难以缠住我的头。
In a spot of gratuitous self-promotion I wrote a series of articles on the basics of WordPress hooks and filters(one article says it is for Thematic hooks, but a hook is a hook! ) that are all the things I wish people had told me at the beginning of my WordPress career.
在一次无偿的自我宣传中,我写了一系列关于WordPress 钩子和过滤器基础知识的文章(一篇文章说它是针对主题钩子的,但钩子就是钩子!)这是我希望人们告诉的所有事情在我的 WordPress 职业生涯开始时。