如果您不打算添加太多html,则无需使用ob_start
只需返回php:
function wpb_description_shortcode( ){
if (get_post_meta(get_the_ID(), \'song_number\', true)) {
return get_post_meta(get_the_ID(), \'song_number\', true).\'<br/>\';
} else {
return;
}
}
// Register shortcode
add_shortcode(\'my_description_number\', \'wpb_description_shortcode\');
我不确定这些数据是什么,但为了安全起见,最好也对其进行清理。
如果您计划添加更多字段,可以这样做:
function wpb_description_shortcode( ){
$output = \'\';
if (get_post_meta(get_the_ID(), \'song_number\', true)) {
$output .= get_post_meta(get_the_ID(), \'song_number\', true).\'<br/>\';
}
if (get_post_meta(get_the_ID(), \'another_field\', true)) {
$output .= get_post_meta(get_the_ID(), \'another_field\', true).\'<br/>\';
}
//random html
$output .= \'<h3> cool html </h3>\';
if (get_post_meta(get_the_ID(), \'even_another\', true)) {
$output .= get_post_meta(get_the_ID(), \'even_another.\', true).\'<br/>\';
}
return $output;
}
// Register shortcode
add_shortcode(\'my_description_number\', \'wpb_description_shortcode\');