我在使用 while 回圈回圈自定義帖子型別時遇到問題。是的,我在顯示資料時沒有錯誤,但the_ID()函式正在特定 id 之外列印,即 id="panelsStayOpen-heading'.the_ID().'" 如圖所示。
問題
function displayFaqs() {
$args = array(
'post_type' => 'advance_faq',
'posts_per_page' => '-1',
'post_status' => 'publish'
);
// The Query
$the_query = new WP_Query( $args );
ob_start();
// The Loop
if ( $the_query->have_posts() ) {
echo '<div id="accordionPanelsStayOpenExample">';
while ( $the_query->have_posts() ) {
$the_query->the_post();
echo '<div >';
echo ' <h2 id="panelsStayOpen-heading'.the_ID().'">
<button type="button" data-bs-toggle="collapse" data-bs-target="#panelsStayOpen-collapse'.the_ID().'" aria-expanded="true" aria-controls="panelsStayOpen-collapse'.the_ID().'">'
. get_the_title() . '
</button>
</h2>';
echo '<div id="panelsStayOpen-collapse'.the_ID().'" aria-labelledby="panelsStayOpen-heading'.the_ID().'">
<div >
'
.the_content() . '
</div>
</div>';
}
echo '</div>';
echo '</div>';
} else {
// no posts found
}
/* Restore original Post Data */
wp_reset_postdata();
$contents = ob_get_contents();
ob_end_clean();
return $contents;
}
add_action( 'init', 'a_register_shortcodes');
如何解決問題以在特定 ID 中列印而不在外部列印
uj5u.com熱心網友回復:
將 the_ID() 替換為 get_the_ID() 因為您已經在使用 echo。
the_ID() 用于直接列印出 post_id,get_the_ID() 回傳 id。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/528413.html
