简体   繁体   English

将 html 添加到 woocommerce 产品标题

[英]Add html to woocommerce product title

I want to be able to add an "li" tag within my product title.我希望能够在我的产品标题中添加一个“li”标签。 To achieve this in a user friendly way I wrote a code which changes the character "-" to an "li" tag.为了以用户友好的方式实现这一点,我编写了一个代码,将字符“-”更改为“li”标签。 But currently the html does not have an effect on the "order-details-table" (which for example appears when you finished ordering). 但目前 html 对“订单详细信息表”没有影响(例如,在您完成订购时出现)。 Is there another filter to add the html globaly so it changes "-" to "li" every time the title occures? 是否有另一个过滤器可以添加 html globaly,以便每次标题出现时将“-”更改为“li”? --> I updated my code and the html now appeares everywhere, only the following problem is remaining: -->我更新了我的代码,现在到处都出现了 html,只剩下以下问题:

In the backend however the html gets added, but gets shown as plain text, so it does not have an effect.然而,在后端添加了 html,但显示为纯文本,因此它没有效果。 Is there also a solution to this problem?这个问题也有解决方案吗?

What the product title looks like at the moment --> the html gets interpreted as normal text产品标题目前的样子 --> html 被解释为普通文本

add_filter( 'the_title', 'custom_the_title', 10, 2 );
add_filter( 'woocommerce_cart_item_name', 'custom_the_title', 20, 3);
add_filter( 'woocommerce_order_item_name', 'custom_the_title' );

function custom_the_title( $title){

        $title = str_replace( '-', '<li>', $title );
        $title = str_replace( '.', '</li>', $title );

    return $title;
}

Thanks a lot for your help, and greetings from Austria!非常感谢您的帮助和来自奥地利的问候! Samuel塞缪尔

Are you trying to get something like this?你想得到这样的东西吗? If not, please give more information, what do you expect to see?如果没有,请提供更多信息,您希望看到什么? Any visual example?任何视觉示例? Where do you want to see those changes?你想在哪里看到这些变化?

add_filter( 'the_title', 'custom_the_title', 10, 2 );
add_filter( 'woocommerce_cart_item_name', 'custom_the_title', 20, 3);

function custom_the_title( $title){

        $title = '<li>'.$title.'</li>';
    return $title;
}

The problem is still presisting even on Wordpress version 5.2.4 and not only on Wordpress 5.5;这个问题甚至在 Wordpress 5.2.4 版上仍然存在,而不仅仅是在 Wordpress 5.5 上; I have tried the suggested function but it won't work as the html code will always be shown as part of the title text.我已经尝试了建议的功能,但它不起作用,因为 html 代码将始终显示为标题文本的一部分。 For instance if I want to add a line break of change a word in the title to red the html tags will just show and the html is not rendered by the browser.例如,如果我想添加一个将标题中的单词更改为红色的换行符,则 html 标签将仅显示并且 html 不会由浏览器呈现。

Even with php function html_entity_decode() applied to the title no luck!即使将 php 函数html_entity_decode()应用于标题也没有运气!

So here is the fix:所以这里是修复:

After checking the code on Woocommerce latest update I found the culprit on the titile.php woocommerce plugin code:在检查 Woocommerce 最新更新上的代码后,我在titile.php woocommerce 插件代码中找到了罪魁祸首:

echo esc_html( get_the_title() );

you will need to edit that and remove the esc_html() .您需要编辑它并删除esc_html() But this is temporary as any update on the woocommrce plugin will perhaps return the issue.但这是暂时的,因为 woocommrce 插件的任何更新都可能会返回问题。 So I will leave it to experts here to suggest a better solution.所以我会把它留给专家在这里提出更好的解决方案。

I found the same issue where I'd added some spans to my titles that were suddenly being rendered as text instead of HTML.我发现了同样的问题,我在标题中添加了一些跨度,这些跨度突然呈现为文本而不是 HTML。 It was indeed thanks to the addition of an esc_html() around the title in woocommerce/templates/single-product/title.php.这确实要归功于在 woocommerce/templates/single-product/title.php 中的标题周围添加了esc_html()

I've fixed it by copying the file to my theme folder under woocommerce/single-product/title.php and simply removing the esc_html() .我通过将文件复制到 woocommerce/single-product/title.php 下的主题文件夹并简单地删除esc_html()来修复它。 This is the 'update-proof' solution as it will not be overwritten when WooCommerce next updates.这是“防更新”解决方案,因为它不会在 WooCommerce 下次更新时被覆盖。

<h1 class="product_title entry-title">
    <?php echo get_the_title(); ?>
</h1>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM