wordpress 自定义帖子 wp_query 上的分页
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14595709/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
pagination on custom post wp_query
提问by woninana
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$loop = new WP_Query(
array(
'post_type' => 'html5-blank',
'posts_per_page' => 5,
'paged'=>$paged
)
);
?>
<?php if ($loop->have_posts()): while ($loop->have_posts()) : $loop->the_post(); ?>
//Loop Code Here..
<?php wp_reset_query(); ?>
<nav>
<?php previous_posts_link( 'Newer posts »' ); ?>
<?php next_posts_link('Older »') ?>
</nav>
<?php endwhile; ?>
<?php else: ?>
Url on next page as I input result is: www.mywebsite.com/blog/page/2 is WORKING. But I can't show the pagination links.
当我输入结果时,下一页的网址是:www.mywebsite.com/blog/page/2 is WORKING。但我无法显示分页链接。
Where did it go wrong?
哪里出错了?
EDIT: Pagination link is showing in page/2/
but not in the main blog page.
Why?
编辑:分页链接显示在page/2/
但不在主博客页面中。为什么?
采纳答案by Reigel
I think you put <?php wp_reset_query(); ?>
in the wrong place.. shouldn't it be next or after pagination codes?
我认为你放<?php wp_reset_query(); ?>
错了地方..不应该在分页码的下一个或之后吗?
something like this
像这样的东西
<?php endwhile; ?>
<?php else: ?>
<?php wp_reset_query(); ?>
回答by Trevor
There are 3 ways that I would suggest for pagination with a custom post wp_query. Unfortunately to this day there isn't a lot of good information about this out there, or at least what is out there is unclear in some cases. Hopefully this helps!
我建议使用 3 种方式使用自定义帖子 wp_query 进行分页。不幸的是,直到今天还没有很多关于这方面的好信息,或者至少在某些情况下不清楚。希望这会有所帮助!
Note, you also did have the wp_reset_postdata() in the wrong place, but even still more is needed to get it to work correctly.
请注意,您确实也将 wp_reset_postdata() 放在了错误的位置,但要使其正常工作还需要更多。
Option 1 - use max_num_pages variable
选项 1 - 使用 max_num_pages 变量
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
'posts_per_page' => 1,
'paged' => $paged,
'post_type' => 'cpt_type'
);
$cpt_query = new WP_Query($args);
?>
<?php if ($cpt_query->have_posts()) : while ($cpt_query->have_posts()) : $cpt_query->the_post(); ?>
//Loop Code Here...
<?php endwhile; endif; ?>
<nav>
<ul>
<li><?php previous_posts_link( '« PREV', $cpt_query->max_num_pages) ?></li>
<li><?php next_posts_link( 'NEXT »', $cpt_query->max_num_pages) ?></li>
</ul>
</nav>
You'll see above, a slightly different format for previous_posts_link
and next_posts_link
which now access the max_num_pages
variable. Be sure to use your own query variable name when accessing max_num_pages
. Notice I use $cpt_query since that is the variable for my query example.
你会看到上面的,略有不同的格式previous_posts_link
和next_posts_link
现在访问max_num_pages
变量。访问时请务必使用您自己的查询变量名称max_num_pages
。请注意,我使用 $cpt_query,因为这是我的查询示例的变量。
Option 2 - temporarily use the $wp_query variable for your loop query
选项 2 - 临时使用 $wp_query 变量进行循环查询
This is what a lot of folks recommend, but be careful to asign the $wp_query variable to a temp variable and re-assign it or you will run in to all kinds of troubles. Which is why I recommend Option #1. As noted on CSS Tricks, you can do something like this:
这是很多人推荐的,但要小心将 $wp_query 变量分配给临时变量并重新分配它,否则你会遇到各种麻烦。这就是我推荐选项 #1 的原因。如CSS Tricks 所述,您可以执行以下操作:
<?php
$temp = $wp_query;
$wp_query = null;
$wp_query = new WP_Query();
$wp_query->query('showposts=6&post_type=news'.'&paged='.$paged);
while ($wp_query->have_posts()) : $wp_query->the_post();
?>
<!-- LOOP: Usual Post Template Stuff Here-->
<?php endwhile; ?>
<nav>
<?php previous_posts_link('« Newer') ?>
<?php next_posts_link('Older »') ?>
</nav>
<?php
$wp_query = null;
$wp_query = $temp; // Reset
?>
Option 3 - use WP-pagenavi plugin
选项 3 - 使用 WP-pagenavi 插件
Just as another option what you can do instead is use the WP-pagenaviplugin, and setup your query as in Option #1. But make one change in the code, remove everything within the element and replace with this function, once you have installed the plugin. So you'll end with:
正如您可以做的另一种选择是使用WP-pagenavi插件,并按照选项 #1 设置您的查询。但是在代码中做一个更改,删除元素中的所有内容并替换为这个函数,一旦你安装了插件。所以你会以:
<nav>
<?php wp_pagenavi( array( 'query' => $cpt_query ) ); ?>
</nav>
回答by suraj singh
<?php
add_shortcode('show_all_news', 'show_all_news');
function show_all_news($atts) {
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$limit = 4;
$offset = ( $limit * $paged ) - $limit;
$atts['pages'] = $paged;
$atts['post-type'] = 'your_custom_post_type';
$atts['orderby'] = 'date';
$atts['order'] = 'DESC';
$atts['offset'] = $offset;
$atts['posts_per_page'] = $limit;
$atts['caller_get_posts'] = 1;
$result = new WP_Query($atts);
echo '<div class="news-cont">';
if($result->have_posts())
{
while ($result->have_posts()) : $result->the_post();
echo '<div class="bg-white news-block">';
echo '<em class="date">'. get_the_date().'</em>' ?>
<h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
<?php
echo "<p>";
the_excerpt();
echo "</p>";
echo '</div>';
endwhile;
?>
<ul class="pagination" style="width:100%;">
<li id="previous-posts" style="float:left">
<?php previous_posts_link( '<< Vorige Pagina', $result->max_num_pages ); ?>
</li>
<li id="next-posts" style="float:right">
<?php next_posts_link( 'Volgende Pagina >>', $result->max_num_pages ); ?>
</li>
</ul>
<?php
}
echo '</div>';
wp_reset_query();
}