简体   繁体   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

I want to send an email with contactform 7 dynamic hidden field wordpress plugin, to get dynamic content to the email.我想发送一个带有contactform 7动态隐藏字段wordpress插件的email,以获取email的动态内容。 This is possible while using a shortcode.这在使用简码时是可能的。 So I wrote the shortcode and the function, and it seems like it could work, because on the website, the correct output is displayed, but it doesn't send it with the mail.所以我写了简码和 function,它似乎可以工作,因为在网站上,显示了正确的 output,但它没有随邮件一起发送。 I need to get content from several posts and custom fields by ID displayed as list.我需要按显示为列表的 ID 从多个帖子和自定义字段中获取内容。

It sends the proper content when there is a simple return 'random text';当有一个简单的return 'random text'; But it doesn't send anything with echo for example.但它不会发送任何带有echo的东西,例如。

So how can I get the content created by the function in a way, that it is a simple return , that can be sent?那么我怎样才能以某种方式获得由 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' );

Thanks for any help and tips!感谢您的帮助和提示!

Shortcode output can't be echo'd out, it must be returned, since do_shortcode is used by echo do_shortcode()短代码 output 无法回显,必须返回,因为回显do_shortcode echo do_shortcode()使用 do_shortcode

From the codex:来自法典:

Note that the function called by the shortcode should never produce an output of any kind.请注意,短代码调用的 function 绝不应该产生任何类型的 output。 Shortcode functions should return the text that is to be used to replace the shortcode.简码函数应返回用于替换简码的文本。 Producing the output directly will lead to unexpected results.直接生产 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' );

You can use ob_start() and ob_get_clen();你可以使用 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.

相关问题 如何在php中将动态创建的表作为电子邮件发送 - How to send dynamic created table as email in php jQuery动态输入字段功能不能与PHP foreach一起使用? - JQuery dynamic input field function not working with php foreach? PHP 联系表格 - PHP ContactForm 如何在Wordpress中将变量发送到function.php - How to send variables to function.php in wordpress 如何发送包含文件的数据表,从 PHP ContactForm7 到 Curl - how to send a Data Form including file, from PHP ContactForm7 with Curl 如何返回<div>在 WordPress Function php 文件中</div><div id="text_translate"><p>我正在尝试在返回周围放置一个 div。 不知道怎么...</p><pre> add_filter( 'the_content', function( $content ) { if (is_singular('cda')) { return get_the_term_list( get_the_ID(), 'cda_cat', 'Product:' ).$content; } }, -10);</pre></div> - How to Return <div> in WordPress Function php file PHP - 将 foreach 值发送到 mysql 数据库字段 - PHP - send foreach values to mysql database field 如何在foreach循环中创建动态函数名称? PHP - How make dynamic function name in foreach loop? PHP 如何在 PHP 的 foreach 循环中创建动态函数? - How to create a dynamic function inside foreach loops in PHP? 我如何在PHP中使用foreach从类/函数返回数组 - how can i return array from class/function with foreach in PHP
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM