繁体   English   中英

如何在 PHP 中返回使用“foreach”作为字符串创建的 function 的所有内容,以使用 wordpress 联系表格 7 动态字段发送

[英]How to return everything a function created with 'foreach' as string in PHP to send with wordpress contactform 7 dynamic field

我想发送一个带有contactform 7动态隐藏字段wordpress插件的email,以获取email的动态内容。 这在使用简码时是可能的。 所以我写了简码和 function,它似乎可以工作,因为在网站上,显示了正确的 output,但它没有随邮件一起发送。 我需要按显示为列表的 ID 从多个帖子和自定义字段中获取内容。

当有一个简单的return 'random text'; 但它不会发送任何带有echo的东西,例如。

那么我怎样才能以某种方式获得由 function 创建的内容,它是一个简单的return ,可以发送?

function show_list_function() {
    if(!empty($_SESSION['acts'])){
        foreach($_SESSION['acts'] as $actID){ //this gives the right content, but doesn't send with the mail
            echo get_the_title($actID); 
            the_field('lange', $actID); 
        }    
    } else {
        return 'Nothing selected'; //this is working
    }
}

add_shortcode( 'show_list', 'show_list_function' );

感谢您的帮助和提示!

短代码 output 无法回显,必须返回,因为回显do_shortcode echo do_shortcode()使用 do_shortcode

来自法典:

请注意,短代码调用的 function 绝不应该产生任何类型的 output。 简码函数应返回用于替换简码的文本。 直接生产 output 会导致意想不到的结果。

function show_list_function() {
    // Init $output as something 
    $output = '';
    if(!empty($_SESSION['acts'])){
        foreach($_SESSION['acts'] as $actID){ //this gives the right content, but doesn't send with the mail
            // concatenate the $output string
            $output .= get_the_title($actID); 
            $output .= get_field('lange', $actID); 
        }    
    } else {
        $output = 'Nothing selected'; //this is working
    }
    return $output;
}

add_shortcode( 'show_list', 'show_list_function' );

你可以使用 ob_start() 和 ob_get_clen();

function show_list_function() {

    ob_start();

    if(!empty($_SESSION['acts'])){
        foreach($_SESSION['acts'] as $actID){ //this gives the right content, but doesn't send with the mail
            echo get_the_title($actID); 
            the_field('lange', $actID); 
        }    
    } else {
        echo 'Nothing selected'; //this is working
    }

    $html = ob_get_clean();
    return $html;
}

add_shortcode( 'show_list', 'show_list_function' );

暂无
暂无

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

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