[英]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.