[英]WordPress Loop Takes Custom Meta from other Posts
I used Devin Says' Team Post Type as a base for a custom post type with custom meta: https://github.com/devinsays/team-post-type . 我使用Devin Says的团队帖子类型作为带有自定义元的自定义帖子类型的基础: https : //github.com/devinsays/team-post-type 。 My plugin is virtually unchanged, aside from language updates and added fields. 除了语言更新和添加的字段外,我的插件几乎未更改。
When I output my posts, Custom Meta values that are empty for subsequent posts in the loop take the value of previous posts. 当我输出帖子时,对于循环中后续帖子为空的Custom Meta值将采用先前帖子的值。 So - if the Custom Meta of 'telephone' for Post One is set to '555-555-5555' and the Custom Meta of 'telephone' for Post Two is not set, in my loop, it displays '555-555-555'. 所以-如果第一篇文章的“电话”的自定义元设置为“ 555-555-5555”,而第二篇文章的“电话”的自定义元未设置,则在我的循环中,它显示为“ 555-555-555” '。
So - the HTML output (for example) would look like: 因此-HTML输出(例如)如下所示:
<section>
<h2>Person 1</h2>
<p>555.555.5555</p>
</section>
<section>
<h2>Person 2</h2>
<p>555.555.5555</p>
</section>
... even when there is no phone number in the database for Person 2. ...即使数据库中没有人2的电话号码,也是如此。
I found this similar question which has the same symptom, but did not see a 'solution' that would help me - the recommendations that are stated are things I am doing already, and I do not see a critical difference in the way that I am calling data to the page: Values from custom meta boxes being repeated in posts 我发现了这个症状相同的类似问题,但是没有找到可以帮助我的“解决方案”-提出的建议是我已经在做的事情,并且我认为自己的方式没有重大差异调用数据到页面: 自定义元框中的值在帖子中重复
Does anyone have a suggestion regarding this problem? 有人对这个问题有建议吗?
I'm trying to output my posts using the following: 我正在尝试使用以下方法输出帖子:
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;
}
Technically your script works correctly. 从技术上讲,您的脚本可以正常工作。 Because you never clear the $output_xyz
variables at any time your script assumes the value from the last loop. 因为您永远不会清除$output_xyz
变量,所以脚本会采用上一次循环的值。
Either change your if ( !empty (...) ) ...
to have an else
block like this: 要么更改您的if ( !empty (...) ) ...
以使其具有else
块,如下所示:
if ( !empty ( $phone ) ) {
$output_phone = '<a href="tel:'.$phone.'">...</a>';
} else {
$output_phone = '';
}
Or clear the variables at the beginning of each loop: 或在每个循环开始时清除变量:
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
$query->the_post();
$output_image = '';
$output_phone = '';
...
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.