基本上,我想先显示带有"特色"标签的项目,然后显示所有没有"特色"标记的项目,再对结果进行分页。
$featured = array(
'post_type' => 'aircraft-refueling',
'tag' => 'featured'
);
$not_featured = array(
'post_type' => 'aircraft-refueling',
'tag__not_in' => array('592')
);
这是页码:
function pagination()
{
global $wp_query;
$big = 999999999;
echo paginate_links(array(
'base' => str_replace($big, '%#%', get_pagenum_link($big)),
'format' => '?paged=%#%',
'current' => max(1, get_query_var('paged')),
'total' => $wp_query->max_num_pages
));
}
我不太熟悉PHP,但我猜我需要将查询合并到一个新的查询中,然后对该新查询进行分页?非常感谢您的帮助!如果有更好的方法,也请提出建议。
编辑:这是我的进步。我几乎让它工作了,但我在这个代码中遇到了一个错误,尽管它看起来很好。。。?
<?php
$featured = get_posts(array(
'post_type' => 'aircraft-refueling',
'tag' => 'featured'
));
$notfeatured = get_posts(array(
'post_type' => 'aircraft-refueling',
'tag__not_in' => array('592')
));
$mergedposts = array_merge( $featured, $notfeatured );
$postids = array();
foreach( $mergedposts as $item ) {
$postids[]=$item->ID;
}
$uniqueposts = array_unique($postids);
$posts = get_posts(array(
'post__in' => $uniqueposts,
'post_type' => 'aircraft-refueling',
'post_status' => 'publish'
));
foreach( $posts as $post ) :
setup_postdata($post);
?>
然后标准查询:
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
// THE CONTENT
<?php endwhile; ?>
<?php endif; ?>
从上面提供的代码中,我不知道如何运行显示数据的循环。如果你这样做了:
<?php foreach( $posts as $post ) :
setup_postdata( $post );
endforeach; ?>
那么就不需要第二个循环:
while ( have_posts() ) : the_post(); ?>
// THE CONTENT
<?php endwhile; ?>
因为setup_postdata()
和the_post()
基本上是一样的-他们用循环中的数据设置全局$post
对象,所以像the_title()
、the_ID()
。。。可以正常工作。
所以你必须:
- 如果使用
query_posts()
<?php foreach ( $posts as $post ):
setup_postdata( $post )
//THE CONTENT (the_title(), the_ID() will work
endforeach; ?>
- 如果你使用
WP_Query()
(我认为这是最好的方法)
<?php
$posts = new WP_Query( array(
'post__in' => $uniqueposts,
'post_type' => 'aircraft-refueling',
'post_status' => 'publish'
));
if( $posts->have_posts() ):
while( $posts->have_posts() ): $posts->the_post()
//THE CONTENT (the_title(), the_ID() will work
endwhile;
endif;
?>
在这两种情况下,都必须在循环后执行wp_reset_postdata()
。
PS:在许多情况下,如果您使用custom_post_types
进行查询,这将阻止分页:)
如果是这种情况,请查看此链接以了解想法:css技巧
最后:你需要这张array_unique
支票吗?帖子会有或没有这个标签。。。