使用过滤器将 html 添加到 WooCommerce 商店通知 [英] Adding html to WooCommerce Store Notice using filters
本文介绍了使用过滤器将 html 添加到 WooCommerce 商店通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试使用过滤器在我的 WooCommerce 商店通知周围添加一个 div 包装器.而且我还想用关闭图标替换关闭链接.
I am trying to add a div wrapper around my WooCommerce Store Notice using a filter. And I also want to replace the dismiss link with a close icon.
这就是我目前所拥有的,它并没有像我想要的那样工作;
This is what I have so far and it doesn't really work how I want it to;
add_filter('woocommerce_demo_store', 'demo_store_filter');
function demo_store_filter($text){
return str_replace( '<p class="woocommerce-store-notice demo_store">', '<div class="hello"><p class="woocommerce-store-notice demo_store"></p></div>', $text);
}
这里是通知的默认html;
Here is the default html for the notice;
<p class="woocommerce-store-notice demo_store">Enter the code <strong>'TLFS5V'</strong> to received £10 off your order when you spend over £100 <a href="#" class="woocommerce-store-notice__dismiss-link">Dismiss</a></p>
这是我想要的;
<div class="container-fluid"><div class="container"><p class="woocommerce-store-notice demo_store">Enter the code <strong>'TLFS5V'</strong> to received £10 off your order when you spend over £100 <a href="#" class="woocommerce-store-notice__dismiss-link">(close svg icon here)</a></p></div></div>
这可以通过过滤器实现吗?或者我应该通过创建一个新函数来做到这一点?
Is this possible with filters? Or should I do this by creating a new function?
推荐答案
你可以这样做:
add_filter('woocommerce_demo_store', 'demo_store_filter', 10, 1);
function demo_store_filter($text)
{
$text = str_replace(array('<p class="woocommerce-store-notice demo_store">', '</p>', 'Dismiss'), array('<div class="hello"><p class="woocommerce-store-notice demo_store">', '</p></div>', '(close svg icon here)'), $text);
return $text;
}
输出:
<div class="hello"><p class="woocommerce-store-notice demo_store">as <a href="#" class="woocommerce-store-notice__dismiss-link">(close svg icon here)</a></p></div>
经过测试和工作.
这篇关于使用过滤器将 html 添加到 WooCommerce 商店通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文