我必须获得一些自定义的帖子类型,并且需要使用WP\\u Query(Query\\u帖子不起作用)。
如何分页?不管我怎么做都没用。。。任何帮助都太棒了,我无法独自解决这个问题。。。
$args = array(
\'tax_query\' => array(
\'posts_per_page\' => 5,
array(
\'author\' => $user_id,
\'taxonomy\' => \'custom_taxtype\',
\'field\' => \'slug\',
\'terms\' => \'delicious-food\',
)
)
);
$answer_query = new WP_Query( $args );
//// Loop trough the results
while ( $answer_query->have_posts() ) : $answer_query->the_post();
//// do something
endwhile;
最合适的回答,由SO网友:Rutwick Gangurde 整理而成
如果你能告诉我们你到底想要实现什么,我们可以更好地帮助你!
<?php
$paged = get_query_var(\'paged\') ? get_query_var(\'paged\') : 1;
$args = array(
\'post_type\' => \'question\',
\'posts_per_page\' => -1,
\'paged\' => $paged,
\'author\' => $user_id
);
$my_query = new WP_Query($args);
if($my_query->have_posts()):
while($my_query->have_posts()):$my_query->the_post();
//Loop goes here...
endwhile;
//support for page-navi plugin, please refer readme.txt for further instructions
if ( function_exists(\'wp_pagenavi\') )
{
wp_pagenavi();
}
elseif ( get_next_posts_link() || get_previous_posts_link() )
{
?>
<div class="wp-navigation clearfix">
<div class="alignleft"><?php //next_posts_link(\'« Older Entries\'); ?></div>
<div class="alignright"><?php //previous_posts_link(\'Newer Entries »\'); ?></div>
</div>
<?php } //if wp_pagenavi
endif;
?>
检查
WP_Query documentation 获取更多参数。
顺便提一下query_posts
还可用于自定义帖子,因为它接受可以传递到WP\\u Query的所有参数。