【问题标题】:Concatonating Strings with PHP & HTML in Wordpress Woocommerce Function在 Wordpress Woocommerce 函数中使用 PHP 和 HTML 连接字符串
【发布时间】:2014-07-18 15:01:11
【问题描述】:

我正在尝试在将字符串分配给 PHP 中的变量之前为其添加一些标记。

Wordpress/Woocomerce 使用“Hooks”系统来改变默认行为。在这种情况下,我试图为文本字段分配一个新标签。我目前有:

<?php
add_filter( 'woocommerce_checkout_fields' , 'custom_override_checkout_fields' );

function custom_override_checkout_fields( $fields ) {
    $fields['billing']['billing_phone']['label'] = 'My New Label';
     return $fields;
}
?>

将文本设置为My New Label 效果很好,但是我真正想要做的是使用 HTML/CSS 设置标签的样式(例如,让它读取“我的新 标签” ) 所以我需要转义 PHP,但似乎没有任何效果。

我试过了:

$fields['billing']['billing_phone']['label'] = 'My New' ?&gt; &lt;em&gt; &lt;?php .='Label'?&gt;&lt;/em&gt;&lt;?php;

我也试过了:

$fields['billing']['billing_phone']['label'] = echo "My New &lt;em&gt;Label&lt;/em&gt;";

还有:

$fields['billing']['billing_phone']['label'] = echo 'My New &lt;em&gt;Label&lt;/em&gt;';

出于绝望,我什至尝试过:

$fields['billing']['billing_phone']['label'] = 'My New ?&gt; &lt;em&gt; &lt;?php Label ?&gt;&lt;/em&gt;&lt;?php';

我错过了什么?一定有办法做到这一点!

【问题讨论】:

  • 3 件事情浮现在脑海中,你可以检查:1)关闭缓存如果打开,例如 WP Super Cache 或 Total Cache 2)css 样式可能会覆盖文本的外观。使用查看源代码或开发者控制台检查源代码。 3) 它可能被编辑在错误的位置,并且可能是不同页面的标签,因为 Woo Commerce 有很多情况需要处理。但看起来不错。

标签: php html css wordpress string-concatenation


【解决方案1】:

你为什么跳出 PHP 模式?这种难以理解的声明是没有理由的。为什么不只是

$foo = 'My new <em>Label</em>';

?

同样,您需要对echo 进行RTM。这不是一个函数。它没有返回值。你永远不能以你尝试的方式使用它。

【讨论】:

  • 现在我觉得自己很愚蠢。当然,这很好用。我假设输出实际上是:My new &lt;em&gt;Label&lt;/em&gt; 因为我认为您必须转义 php 才能拥有任何类型的 html。我需要对与 PHP 相关的所有内容进行 RTFM,因为它对我来说是全新的,而且我是一个糟糕的程序员。谢谢
【解决方案2】:

这应该适用于您所描述的内容。看起来你没有连接任何 PHP

$fields['billing']['billing_phone']['label'] = 'My New<em>Label</em>';

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-09-15
    • 2017-11-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-11
    • 2010-12-02
    相关资源
    最近更新 更多