您正在查询doctors
键入,并将每页的帖子数设置为1。显然,这将始终返回相同的帖子。
您需要设置帖子的id,或者在循环中导航以找到匹配项。如果您在单个帖子页面中调用此函数,要根据帖子id筛选结果,应按以下方式使用此函数:
function doctor_banner_shortcode($doctor_id) {
$args = array(
\'posts_per_page\' => 1,
\'post_type\' => \'doctors\',
\'post_status\' => \'publish\',
\'p\' => $doctor_id
);
$doctors_query = new WP_Query( $args );
if ( $doctors_query->have_posts() ) :
while ( $doctors_query->have_posts() ) :
$doctors_query->the_post();
$dr_name = get_field( \'practitioner_name\' );
$dr_degree = get_field( \'practitioner_title\' );
$dr_bio = get_field( \'practitioner_short_bio\' );
$dr_cv = get_field( \'practitioner_cv\' );
$html_out = \'<h1>Dr. \' . $dr_name . \'</h1>
<h3">\' . $dr_degree . \'</h3>
<p>\' . $dr_bio . \'</p>
<a href="\' . $dr_cv . \'">\' . \'Read CV\' . \'</a>\';
endwhile;
else : // No results
$html_out = "No Doctors Found.";
endif;
wp_reset_query();
return $html_out;
}
add_shortcode( \'doctor_banner\', \'doctor_banner_shortcode\' );
然后可以在
single.php
模板如下:
function doctor_banner_shortcode($post->ID)
如果要在其他页面中手动调用该函数,则应在调用该函数时设置ID。