[英]WordPress Loop Takes Custom Meta from other Posts
我使用Devin Says的團隊帖子類型作為帶有自定義元的自定義帖子類型的基礎: https : //github.com/devinsays/team-post-type 。 除了語言更新和添加的字段外,我的插件幾乎未更改。
當我輸出帖子時,對於循環中后續帖子為空的Custom Meta值將采用先前帖子的值。 所以-如果第一篇文章的“電話”的自定義元設置為“ 555-555-5555”,而第二篇文章的“電話”的自定義元未設置,則在我的循環中,它顯示為“ 555-555-555” '。
因此-HTML輸出(例如)如下所示:
<section>
<h2>Person 1</h2>
<p>555.555.5555</p>
</section>
<section>
<h2>Person 2</h2>
<p>555.555.5555</p>
</section>
...即使數據庫中沒有人2的電話號碼,也是如此。
我發現了這個症狀相同的類似問題,但是沒有找到可以幫助我的“解決方案”-提出的建議是我已經在做的事情,並且我認為自己的方式沒有重大差異調用數據到頁面: 自定義元框中的值在帖子中重復
有人對這個問題有建議嗎?
我正在嘗試使用以下方法輸出帖子:
add_shortcode('staff-list', 'jpro_staff_list');
function jpro_staff_list($atts) {
extract(shortcode_atts(array(
), $atts));
$args = array (
'post_type' => array( 'staff' ),
'posts_per_page' => '-1',
'orderby' => 'menu_order',
'order' => 'ASC'
);
$query = new WP_Query( $args );
$staff_list = '<div class="jpro-staff staff-list container">';
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
$query->the_post();
$post = get_post($id);
$postID = $post->ID;
$image = get_the_post_thumbnail( $postID, 'medium', array( 'class' => 'aligncenter' ));
$name = get_the_title($postID);
$bio = apply_filters('the_content', $post->post_content);
$title = get_post_meta($postID, 'staff_title', true);
$video = get_post_meta($postID, 'staff_video', true);
$email = get_post_meta($postID, 'staff_email', true);
$phone = get_post_meta($postID, 'staff_phone', true);
$fax = get_post_meta($postID, 'staff_fax', true);
if(!empty($image)){
$output_image = '<div class="staff-list image">'.$image.'</div>';
}
if(!empty($name)){
$output_name ='<h3 class="staff-name staff-list">'.$name.'</h3>';
}
if(!empty($bio)){
$output_bio ='<div class="staff-bio staff-list">'.$bio.'</div>';
}
if(!empty($video)){
$output_video ='
<div class="staff-video-container">
<iframe class="staff-video staff-list" src="//www.'.$video.'" width="560" height="315" frameborder="0" allowfullscreen="allowfullscreen">
</iframe>
</div>';
}
if(!empty($title)){
$output_title ='<li class="staff-title staff-list">'.$title.'</li>';
}
if(!empty($email)){
$output_email ='
<a href="mailto:'.$email.'">
<li class="staff-email staff-list"><i class="fa fa-envelope"></i> '.$email.'</li>
</a>';
}
if(!empty($phone)){
$output_phone ='
<a href="tel:'.$phone.'">
<li class="staff-phone staff-list"><i class="fa fa-phone-square"></i> '.$phone.'</li>
</a>';
}
if(!empty($fax)){
$output_fax ='<li class="staff-fax staff-list"><i class="fa fa-fax"></i> '.$fax.'</li>';
}
$staff_list .='<section class="jpro-staff staff-list">';
$staff_list .= $output_name;
$staff_list .= $output_video;
$staff_list .= '<ul class="staff-meta staff-list">';
$staff_list .= $output_image;
$staff_list .= $output_name;
$staff_list .= $output_title;
$staff_list .= $output_email;
$staff_list .= $output_phone;
$staff_list .= $output_fax;
$staff_list .= '</ul>';
$staff_list .= $output_bio;
$staff_list .= '</section>';
}
} else {
$staff_list .='<p>Ooops! No Staff Found.</p>';
}
$staff_list .='</div>';
wp_reset_postdata();
return $staff_list;
}
從技術上講,您的腳本可以正常工作。 因為您永遠不會清除$output_xyz
變量,所以腳本會采用上一次循環的值。
要么更改您的if ( !empty (...) ) ...
以使其具有else
塊,如下所示:
if ( !empty ( $phone ) ) {
$output_phone = '<a href="tel:'.$phone.'">...</a>';
} else {
$output_phone = '';
}
或在每個循環開始時清除變量:
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
$query->the_post();
$output_image = '';
$output_phone = '';
...
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.