这似乎是一件容易的事情,但我在这里错过了一些东西。我有一个名为“收件人”的自定义帖子类型和一个名为“收件人”的元复选框yes_current
“-(我们帮助患有癌症的儿童并为他们举办活动,因此我们需要知道谁是当前的接受者,谁是过去的接受者。)我们希望在收件人页面上显示两个部分:当前收件人和过去的收件人。
我的想法是查询循环以获取custom_post_types
该相等的收件人,然后在当前部分中显示所有当前收件人的特征图像(如果“yes\\u CURRENT”处于启用状态),然后在过去部分中对过去的收件人执行相同操作。
谢谢大家!
代码:
<h1>CURRENT Recipients</h1>
<?php $current_loop = new WP_Query(array(\'post_type\' => \'recipient\', \'posts_per_page\' => \'-1\', \'yes_current\' => \'on\'));
if( $current_loop->have_posts() ): while( $current_loop->have_posts() ): $current_loop-the_post();
<?php get_the_post_thumbnail($page->ID, \'thumbnail\'); ?>
<?php endwhile; endif; wp_reset_postdata(); ?>
<h1>PAST Recipients</h1>
<?php $current_loop = new WP_Query(array(\'post_type\' => \'recipient\', \'posts_per_page\' => \'-1\', \'yes_current\' => \'off\'));
if( $current_loop->have_posts() ): while( $current_loop->have_posts() ): $current_loop->the_post();
<?php get_the_post_thumbnail($page->ID, \'thumbnail\'); ?>
<?php endwhile; endif; wp_reset_postdata(); ?>
编辑自定义post元代码:
$meta_boxes[] = array( \'id\' => \'yes_current\', \'title\' => \'Current Recipient\', \'pages\' => array( \'recipient\' ), \'context\' => \'side\', \'priority\' => \'low\', \'fields\' => array( array( \'name\' => \'Current Recipient\', \'id\' => "{$prefix}yes_current", \'type\' => \'checkbox\', // Value can be 0 or 1 \'std\' => 1, ), ),
最合适的回答,由SO网友:Chris 整理而成
所以我想出来了。因为我使用的是Deluxe Blog Tips上的Meta框,所以我使用了他们的helper函数来代替get\\u post\\u Meta。以下是显示“我的自定义\\u post\\u类型收件人”的当前收件人列表和过去收件人列表的最后一段代码。要与值进行比较的元框为rec\\u yes\\u current:
<h1>Current Recipients</h1>
<?php /* Start loop */ ?>
<?php $linkposts = new WP_Query(array(\'post_type\' => \'recipient\', \'posts_per_page\' => \'-1\'));
while ($linkposts->have_posts()) : $linkposts->the_post();
$linkvalue = $linkposts->ID;
$image = wp_get_attachment_image_src( get_post_thumbnail_id($linkvalue), \'post-thumbnail\' );
$medical = get_post_meta(get_the_id(), \'rec_medical\', true);
$twitter = get_post_meta(get_the_id(), \'rec_twitter\', true);
$facebook = get_post_meta(get_the_id(), \'rec_facebook\', true);
$dob = get_post_meta(get_the_id(), \'rec_dob\', true);
$today = date("m/d/Y");
list($m,$d,$y) = explode(\'/\',$dob);
list($tm,$td,$ty) = explode(\'/\',$today);
$year=$ty-$y;
$current = rwmb_meta( \'rec_yes_current\' );
?>
<?php // if is current
if($current == \'\') {
echo \'<div class="single recipient">\';
echo \'<p class="recname"><a href="\' . get_permalink($linkvalue) . \'">\' . get_the_title($linkvalue) . \'</a></p>\';
echo \'<a href="\' . get_permalink($linkvalue) . \'" class="frame">\' . get_the_post_thumbnail($linkvalue, \'thumbnail\') . \'</a>\';
echo \'<p class="year"><strong>Age</strong>: \' . $year . \' years old</p>\';
echo \'<p class="condition"><strong>Conditions</strong>:\' . $medical . \'</p>\';
echo \'<p class="readmore"><a href="\' . get_permalink($linkvalue) . \'">read more</a></p>\';
echo \'</div>\';
}; ?>
<h1>Past Recipients</h1>
<?php // if is current
if($current != \'\') {
echo \'<div class="single recipient">\';
echo \'<p class="recname"><a href="\' . get_permalink($linkvalue) . \'">\' . get_the_title($linkvalue) . \'</a></p>\';
echo \'<a href="\' . get_permalink($linkvalue) . \'" class="frame">\' . get_the_post_thumbnail($linkvalue, \'thumbnail\') . \'</a>\';
echo \'<p class="year"><strong>Age</strong>: \' . $year . \' years old</p>\';
echo \'<p class="condition"><strong>Conditions</strong>:\' . $medical . \'</p>\';
echo \'<p class="readmore"><a href="\' . get_permalink($linkvalue) . \'">read more</a></p>\';
echo \'</div>\';
}; ?>
<?php endwhile; wp_reset_query(); ?>
如果有更干净/更好的方法来写这篇文章,我很乐意看到。谢谢大家!