【问题标题】:how to assign css class to a php varible inside an html string?如何将css类分配给html字符串中的php变量?
【发布时间】:2020-05-12 15:41:05
【问题描述】:

我必须在 html 字符串中使用 php 变量,我使用的是 woocommerce,变量是 $cannot_ship

echo wp_kses_post( 
    apply_filters( 
        'woocommerce_cart_no_shipping_available_html', 
        sprintf( esc_html__( 'We\'re Sorry, "'.$cannot_ship.'" in your cart can\'t be shipped for %s.', 'woocommerce' ) . ' ', '<strong>' . esc_html( $formatted_destination ) . '</strong>' ) 
    ) 
); 

请指导如何向这个变量添加一个类。

【问题讨论】:

  • HTML 中不能“给变量添加类”,只能给元素添加类。因此,如果您希望 $cannot_shiponly 的值能够获得特殊格式或类似的东西,那么您需要先将其包装到 HTML 元素中。
  • 你能告诉我该怎么做吗
  • 您到底需要知道什么?将&lt;span&gt; 或其他内容放入您已经拥有的字符串中,听起来不是什么大问题……? (WP 是否允许你在这个地方使用 HTML,或者将它转换为纯文本 - 现在这将是一个不同的问题。)

标签: php html woocommerce


【解决方案1】:

首先将值赋给 $cannot_ship 的新变量 像这样$cannot_ship = '&lt;span class="video-duration" style="color: red;font-family: roboto;font-weight: bold;"&gt;'.substr($cannot_ship, 0, -2). '&lt;/span&gt;'; 然后更改esc_html__ 只需__,您将拥有您的html标签,我使用的情况是跨度问题是由esc_html__引起的,它正在删除我的html标签。

【讨论】:

    猜你喜欢
    • 2011-02-25
    • 2013-04-18
    • 2019-08-19
    • 2018-10-08
    • 1970-01-01
    • 2020-10-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多