我在使用while循环循环自定义帖子类型时遇到问题。是的,我显示数据时没有错误,但the_ID()
函数在特定ID外部打印,即,e id ="panelsStayOpen-heading'. the_ID().'",如图像所示。issue
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 class="accordion" id="accordionPanelsStayOpenExample">';
while ( $the_query->have_posts() ) {
$the_query->the_post();
echo '<div class="accordion-item">';
echo ' <h2 class="accordion-header" id="panelsStayOpen-heading'.the_ID().'">
<button class="accordion-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().'" class="accordion-collapse collapse show" aria-labelledby="panelsStayOpen-heading'.the_ID().'">
<div class="accordion-body">
'
.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中打印而不在外部打印的问题
1条答案
按热度按时间t98cgbkg1#
将_ID()替换为get_the_ID(),因为您已经在使用echo。
用于直接打印出post_id的_ID()和返回id的get_the_ID()。