我想显示所有代理a-z,除了有一个代理应该总是最后出现。理想情况下,我希望从自定义字段dName的值进行排序。
我环顾四周,似乎meta\\u查询是实现这一点的最佳新方法,但尚未找到答案。
显示最近创建的10个代理的当前代码。
<?php $loop = new WP_Query( array( \'post_type\' => \'agents\', \'posts_per_page\' => 10 ) ); ?>
感谢您的关注。
编辑2011年3月27日,工作代码
<?php
$loop = new WP_Query( array(
\'post_type\' => \'agents\',
\'orderby\' => \'meta_value\',
\'meta_key\' => \'rw_dname\',
\'order\'=>\'ASC\',
\'meta_query\' => array(
array( \'key\' => \'rw_dName\' )
),
\'post__not_in\' => array( \'98\' ),
\'posts_per_page\' => -1 ) );
$loop2 = new WP_Query( array(
\'post_type\' => \'agents\',
\'post__in\' => array( \'98\' ) ) );?>
<?php
if($loop->have_posts() || $loop2->have_posts()) {
if($loop->have_posts()) { while($loop->have_posts()) { $loop->the_post();?>
<div class="agentContent">
<div class="agentThumbnail">
<?php
if ( has_post_thumbnail() ) { // check if the post has a Post Thumbnail assigned to it.
the_post_thumbnail();
}?>
</div>
<div class="agentInfo"> <span class="agentInfoEntry">
<?php
$meta = get_post_meta(get_the_ID(), \'rw_dname\', true);
echo $meta; // if you want to show
?>
</span> </div>
</div>
<?php
} //endwhile
}
?>
<?php
if($loop2->have_posts()) { while($loop2->have_posts()) { $loop2->the_post();?>
<div class="agentContent">
<div class="agentThumbnail">
<?php
if ( has_post_thumbnail() ) { // check if the post has a Post Thumbnail assigned to it.
the_post_thumbnail();
}
?>
</div>
<div class="agentInfo"> <span class="agentInfoEntry">
<?php
$meta = get_post_meta(get_the_ID(), \'rw_dname\', true);
echo $meta; // if you want to show
?>
</span> </div>
</div>
<?php
} //endwhile
}
?>
<?php
} else {
echo \'No Agents.\';
}
?>
最合适的回答,由SO网友:Evan Yeung 整理而成
我只是写了这篇文章,所以我没有测试它,但这就是我将如何在最后强制一篇文章。在第一个循环中,它按ID号排除帖子,在第二个循环中,它只按ID号包括帖子,基本上您需要知道帖子的ID。
<?php
$loop = new WP_Query( array(
\'post_type\' => \'agents\',
\'orderby\' => \'meta_value\',
\'meta_key\' => \'dName\',
\'post__not_in\' => array( 00 ),
\'posts_per_page\' => -1 ) );
$loop2 = new WP_Query( array(
\'post_type\' => \'agents\',
\'post__in\' => array( 00 ) ) );
if($loop->have_posts() || $loop2->have_posts()) {
if($loop->have_posts()) { while($loop->have_posts()) { $loop->the_post();
echo the_title();
echo get_post_custom_values(\'dName\');
//Post Content
} //endwhile
}
if($loop2->have_posts()) { while($loop2->have_posts()) { $loop2->the_post();
echo the_title();
echo get_post_custom_values(\'dName\');
//Post Content
} //endwhile
}
} else {
echo \'No Agents.\';
}
?>