我制作了一個自定義的 posttype 并且正在使用 ACF 中的欄位。我制作了這個回圈,它在前端運行良好..,但是在 CPT 中發送帖子時,它給出了錯誤:已發送標題。錯誤是指 if( $posts ) 陳述句正下方的行。
這里有什么問題?
add_shortcode("custom_acf", "event_loop_shortcode");
function event_loop_shortcode() {
$posts = get_posts(array(
'posts_per_page' => -1,
'post_type' => 'event'
));
if( $posts )
echo '<div ><div ><h3>';
the_title();
echo '</h3></div><div >';
the_field("dato");
echo '</div></div>';
}
uj5u.com熱心網友回復:
短代碼函式應該回傳內容而不是回顯它。另外,我認為您的if宣告缺少大括號。
add_shortcode("custom_acf", "event_loop_shortcode");
function event_loop_shortcode() {
$posts = get_posts(array(
'posts_per_page' => -1,
'post_type' => 'event'
));
if ( $posts ) {
ob_start();
echo '<div ><div ><h3>';
the_title();
echo '</h3></div><div >';
the_field("dato");
echo '</div></div>';
$result = ob_get_clean();
} else {
$result = '';
}
return $result;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/368646.html
下一篇:如何使用回圈制作一排三角形?
