简体   繁体   English

php字符串在echo div标签内

[英]php string inside echo div tags

I'm trying to add a return policy custom field just above the add to cart button in woocommerce. 我试图在woocommerce中的添加到购物车按钮上方添加退货政策自定义字段。 I've got the following function: 我有以下功能:

<?php
add_action( 'woocommerce_single_product_summary', 'return_policy', 20 );
function return_policy() {
    echo '<div id="return-policy-wrapper">
            <?php the_cfc_field('rp-info-meta', 'rp-info-custom-filed'); ?>
          </div>';
}

But the code validator points out there is an error somewhere in the string. 但是代码验证器指出字符串中某处有错误。 I suspect the the error is with the single quote marks inside <?php the_cfc_field('rp-info-meta', 'rp-info-custom-filed'); ?> 我怀疑错误是由<?php the_cfc_field('rp-info-meta', 'rp-info-custom-filed'); ?>的单引号引起的<?php the_cfc_field('rp-info-meta', 'rp-info-custom-filed'); ?> <?php the_cfc_field('rp-info-meta', 'rp-info-custom-filed'); ?>

I changed the single quotes in that string for double quotes. 我将字符串中的单引号更改为双引号。 Now the string validation error is gone, but the function won't work. 现在,字符串验证错误已消失,但该功能将无法使用。

Are they the single quotes that are causing the error and how can I fix it? 它们是引起错误的单引号,我该如何解决?

You're already in a <?php ... ?> context. 您已经在<?php ... ?>上下文中。 Simply build your string. 只需构建您的字符串。 For example 例如

printf('<div id="return-policy-wrapper">%s</div>',
        get_cfc_field('rp-info-meta', 'rp-info-custom-filed'));

or 要么

echo '<div id="return-policy-wrapper">',
     get_cfc_field('rp-info-meta', 'rp-info-custom-filed'),
     '</div>';

Note, I've used get_cfc_field instead so the string is returned and not echo -ed directly. 注意,我改用了get_cfc_field所以返回了字符串,而不是直接echo -ed。


Another approach would be 另一种方法是

echo '<div id="return-policy-wrapper">';
the_cfc_field('rp-info-meta', 'rp-info-custom-filed'); // this echoes the value
echo '</div>'

The script is in the quoted string passed to the echo command, and is therefore not treated as a script, but echoed along with the rest of the string. 该脚本位于传递给echo命令的带引号的字符串中,因此不会被视为脚本,而是会与字符串的其余部分一起回显。

This might work as intended: 这可能会按预期工作:

<?php
add_action('woocommerce_single_product_summary', 'return_policy', 20);
function return_policy() {
    $info = get_cfc_field('rp-info-meta', 'rp-info-custom-field');
    echo '<div id="return-policy-wrapper">';
    echo $info;
    echo '</div>';
}

?>

You can't use <?php inside another <?php [...] ?> block. 您不能在另一个<?php [...] ?>块中使用<?php

You code should be similar to that: 您的代码应与此类似:

<?php
add_action( 'woocommerce_single_product_summary', 'return_policy', 20 );
function return_policy() {
    echo '<div id="return-policy-wrapper">' . the_cfc_field('rp-info-meta', 'rp-info-custom-filed') . '</div>';
}

If you see carefully, HTML content is inside single quotes and never contains other single quoted characters (only double ones). 如果仔细看,HTML内容在单引号内,从不包含其他单引号字符(仅双引号)。 Then, I concat the HTML text with the the_cfc_field() function that returns a string and then concat back with more html. 然后,我Concat的与HTML文本the_cfc_field()这个函数返回一个字符串 ,然后CONCAT回来更多的HTML。

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

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