【发布时间】:2016-01-04 11:43:09
【问题描述】:
我使用 Devin Says 的团队帖子类型作为具有自定义元的自定义帖子类型的基础:https://github.com/devinsays/team-post-type。除了语言更新和添加的字段外,我的插件几乎没有变化。
当我输出我的帖子时,循环中后续帖子为空的自定义元值会采用之前帖子的值。所以 - 如果帖子一的“电话”的自定义元设置为“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 个人的电话号码。
我发现了这个具有相同症状的类似问题,但没有看到对我有帮助的“解决方案” - 所陈述的建议是我已经在做的事情,我看不出方式上有什么重大区别我正在向页面调用数据:Values from custom meta boxes being repeated in posts
有人对这个问题有什么建议吗?
我正在尝试使用以下内容输出我的帖子:
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;
}
【问题讨论】:
-
你的简码写得不好,有很多错误。
-
另外,
$id是未定义的,又一个错误添加到其他许多错误中 ;-)
标签: php wordpress custom-post-type