<?php $temp_query = $wp_query; ?>
<?php query_posts('tag=sometag,anothertag&posts_per_page=10'); ?>
<?php while (have_posts()) : the_post(); ?>
// print post here
<?php endwhile; ?>
<?php $wp_query = $temp_query; ?>
使用这个简单的wordpress循环,我如何只显示以字母'G'开头的帖子(实际发布标题)。我想按字母顺序对帖子进行排序,但只对匹配的帖子进行排序,而不是全部。
谢谢!
答案 0 :(得分:5)
我会为查询设置一个操作。在你的主题functions.php文件中:
add_action( 'posts_where', 'startswithaction' );
function startswithaction( $sql ){
global $wpdb;
$startswith = get_query_var( 'startswith' );
if( $startswith ){
$sql .= $wpdb->prepare( " AND $wpdb->posts.post_title LIKE %s ", $startswith.'%' );
}
return $sql;
}
然后您可以像这样查询帖子:
query_posts( 'startswith=G&posts_per_page=10' );
答案 1 :(得分:3)
检查循环内的帖子标题:
while (have_posts()) : the_post();
// jump to the next post if this one doesn't start with the letter you want
if($post->post_title[0] != $letter) continue
// do what you want with the post
endwhile;
答案 2 :(得分:0)
疯狂想到这里,但为什么不把这封信作为标签添加到帖子中。换句话说,如果你想让你的帖子“美丽的树”显示在“B”下(注意我说B而不是T),只需应用名为“B”的标签即可。然后在你查询帖子标签部分,只要确保你附加你的选择信!