Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/wordpress/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何在短代码中显示自定义字段(变量)_Php_Wordpress - Fatal编程技术网

Php 如何在短代码中显示自定义字段(变量)

Php 如何在短代码中显示自定义字段(变量),php,wordpress,Php,Wordpress,我想有一个快捷码显示一个值,这是一个自定义字段在Wordpress。在本例中,变量“prijs”。 我在网上尝试了很多解决方案,还有很多,但到目前为止运气不好。有人能帮我吗? 为什么这个脚本没有显示任何内容?如何显示自定义字段“prijs” } 添加_短代码(“showdetails”、“showdetails_短代码”); ?> 为什么这个脚本没有显示任何内容 提供的代码显示了几个语法错误,其中最关键的是重新调用请注意,由短代码调用的函数不应产生任何类型的输出。短代码函数应返回用于替换短代码

我想有一个快捷码显示一个值,这是一个自定义字段在Wordpress。在本例中,变量“prijs”。 我在网上尝试了很多解决方案,还有很多,但到目前为止运气不好。有人能帮我吗? 为什么这个脚本没有显示任何内容?如何显示自定义字段“prijs”


}
添加_短代码(“showdetails”、“showdetails_短代码”);
?>
为什么这个脚本没有显示任何内容


提供的代码显示了几个语法错误,其中最关键的是重新调用
请注意,由短代码调用的函数不应产生任何类型的输出。短代码函数应返回用于替换短代码的文本。直接生成输出将导致意外的结果。这类似于过滤器函数的行为方式,因为它们不应该在调用时产生预期的副作用,因为您无法控制从何时何地调用它们。“太棒了!它可以工作。非常感谢。您为我节省了很多挫折和时间:-),但它不适用于2(想要构建一个包含所有30个自定义字段的完整表)---$post_id=get_the_id();$cp_prijs=get_post_meta($post_id,'prijs',true)?get_post_meta($post_id,'prijs',true):'NO custom VALUE FOUND;$cp_prijs;-----$cp_merk=get_post_meta($post_id,'merk',true)?get_post__meta($post_id,'merk',true):'NO CUSTOM VALUE FOUND';return$cp_merk;对于您的快捷码函数,您只能
return
一次。与任何其他PHP函数一样,
return
在遇到时停止处理。我为答案添加了更多内容,但我不建议您仅将其扩展为处理30个字段。另一方面,如果您能够使其正常工作,您可以至少在你完成更多的PHP学习之前,我会一直坚持下去。另外,由于我回答了最初的问题,请将我的答案标记为好!答案不好。它非常快,完全正确。我很高兴。非常感谢!
<?php
function showdetails_shortcode( $attr, $content = null ) {
    return <?php $key="prijs"; echo get_post_meta($post->ID, $key, true); ?>
}
add_shortcode('showdetails', 'showdetails_shortcode');
?>
function showdetails_shortcode( ) {

   $post_id = get_the_ID();

//either output the value or let us know the code is working but the value has not been found
   $output = get_post_meta( $post_id, 'prijs', true) ? get_post_meta( $post_id, 'prijs', true) : 'NO CUSTOM VALUE FOUND' ;

   return $output;

}

add_shortcode('showdetails', 'showdetails_shortcode');
function showdetails_shortcode( ) {

   $post_id = get_the_ID();

   //extract the field values
   $field1 = get_post_meta( $post_id, 'prijs', true) ? get_post_meta( $post_id, 'prijs', true) : 'PRIJS NOT FOUND';
   $field2 = get_post_meta( $post_id, 'prijs2', true) ? get_post_meta( $post_id, 'prijs2', true) : 'PRIJS2 NOT FOUND';

   //prepare html table output
   $output = '<table><tbody>'; 
   $output .= '<tr><td>' . $field1 . '</td></tr>';
   $output .= '<tr><td>' . $field2 . '</td></tr>'; 
   $output .= '</tbody></table>';

   //return the html    
   return $output;

}

add_shortcode('showdetails', 'showdetails_shortcode');