分页不适用于 wordpress 自定义帖子 [英] Pagination Doesn't work with wordpress custom post
问题描述
分页不适用于 wordpress 自定义帖子.我在下面尝试了这些代码,但没有出现分页.尝试了不同的查询,但没有结果.我是 php 和 word press 的新手.我只是复制和过去的代码.谁能帮帮我吗?到目前为止,我所做的如下.
在function.php中
/*分页*/函数 wpbeginner_numeric_posts_nav() {如果( is_singular() )返回;全局 $wp_query;/** 如果只有 1 页,则停止执行 */if( $wp_query->max_num_pages <= 1 )返回;$paged = get_query_var('paged') ?absint(get_query_var('paged')):1;$max = intval( $wp_query->max_num_pages );/** 将当前页面添加到数组中 */如果 ( $paged >= 1 )$links[] = $paged;/** 将当前页面周围的页面添加到数组中 */如果 ( $paged >= 3 ) {$links[] = $paged - 1;$links[] = $paged - 2;}如果 ( ( $paged + 2 ) <= $max ) {$links[] = $paged + 2;$links[] = $paged + 1;}echo '<div class="navigation"><ul>'."\n";/** 上一篇文章链接 */如果 ( get_previous_posts_link() )printf( '<li>%s</li>' . "\n", get_previous_posts_link());/** 链接到第一页,必要时加上省略号 */如果 ( ! in_array( 1, $links ) ) {$class = 1 == $paged ?' class="active"' : '';printf( '<li%s><a href="%s">%s</a></li>' . "\n", $class, esc_url( get_pagenum_link( 1 ) ), '1');if ( ! in_array( 2, $links ) )echo '<li>...</li>';}/** 链接到当前页面,如有必要,可在任一方向加上 2 个页面 */排序($链接);foreach ( (array) $links as $link ) {$class = $paged == $link ?' class="active"' : '';printf( '<li%s><a href="%s">%s</a></li>' . "\n", $class, esc_url( get_pagenum_link( $link ) ),$链接);}/** 链接到最后一页,必要时加上省略号 */if ( ! in_array( $max, $links ) ) {if ( ! in_array( $max - 1, $links ) )echo '<li>...</li>'."\n";$class = $paged == $max ?' class="active"' : '';printf( '<li%s><a href="%s">%s</a></li>' . "\n", $class, esc_url( get_pagenum_link( $max ) ),$max );}/** 下一篇文章链接 */如果 ( get_next_posts_link() )printf( '<li>%s</li>' . "\n", get_next_posts_link());回声'</ul></div>'."\n";}
在自定义帖子查询中
5, 'post_type'=> 'latestnews');$myposts = get_posts( $args );foreach( $myposts as $post ) : setup_postdata($post);?><div class="page_news"><div class="single_page_news"><h2><?php the_title();?><h2><p><?php the_content();?></p>
<?php endforeach;?><?php wpbeginner_numeric_posts_nav();?>
请帮帮我
您的分页功能仅针对默认主查询设置,不适用于自定义查询.
另外,不要将 get_posts
用于分页查询.用于自定义查询是一个很好的功能,但是一旦您需要分页,它就变得很麻烦.
而是使用 WP_Query
进行分页查询,它很更容易使用.
示例:
$paged = (get_query_var(‘paged’)) ?get_query_var('paged') : 1;$args = 数组('posts_per_page' =>1、'分页' =>$分页,'post_type' =>'你的帖子类型');$q = new WP_Query($args);if($q->have_post()) {while($q->have_posts()) {$q->the_post();//你的循环}//您的分页}wp_reset_postdata();
您可以查看代码以获取额外参数.
您现在需要将分页函数中 $wp_query
的每个实例更改为 $q
以使其工作.
注意一点,您不必调用 $post
全局
编辑
根据您的评论,有一种更简单的方法可以在没有任何自定义查询的情况下实现您的目标
此页面是一个存档页面,旨在显示您的自定义帖子类型 latestnews
.您只需将 archive-custom.php
重命名为 archive-latestnews.php
.请参阅模板层次结构.只需确保在注册帖子类型时将 has_archive
设置为 true
您也不应该在任何类型的存档页面上购买自定义查询的主要查询.如您所见,这总是很麻烦.因此,删除您的自定义查询并将其替换为默认查询
这是您存档页面中应该包含的全部内容
if(have_post()) {而(有帖子()){the_post();//你的循环}//您的分页}
只需再次将 $q
的所有实例改回 $wp_query
.然后一切正常
有关更多信息,您必须查看这篇文章我在 WPSE 上完成的
Pagination Doesn't work with word press custom post. i have tried these code below,but pagination doesn't appear. tried different query but no result. i am new in php and word press.i just copy and past code. can anyone please help me? what i have done so far are below.
in function.php
/*pagination*/
function wpbeginner_numeric_posts_nav() {
if( is_singular() )
return;
global $wp_query;
/** Stop execution if there's only 1 page */
if( $wp_query->max_num_pages <= 1 )
return;
$paged = get_query_var( 'paged' ) ? absint( get_query_var( 'paged' ) ) : 1;
$max = intval( $wp_query->max_num_pages );
/** Add current page to the array */
if ( $paged >= 1 )
$links[] = $paged;
/** Add the pages around the current page to the array */
if ( $paged >= 3 ) {
$links[] = $paged - 1;
$links[] = $paged - 2;
}
if ( ( $paged + 2 ) <= $max ) {
$links[] = $paged + 2;
$links[] = $paged + 1;
}
echo '<div class="navigation"><ul>' . "\n";
/** Previous Post Link */
if ( get_previous_posts_link() )
printf( '<li>%s</li>' . "\n", get_previous_posts_link() );
/** Link to first page, plus ellipses if necessary */
if ( ! in_array( 1, $links ) ) {
$class = 1 == $paged ? ' class="active"' : '';
printf( '<li%s><a href="%s">%s</a></li>' . "\n", $class, esc_url( get_pagenum_link( 1 ) ), '1' );
if ( ! in_array( 2, $links ) )
echo '<li>…</li>';
}
/** Link to current page, plus 2 pages in either direction if necessary */
sort( $links );
foreach ( (array) $links as $link ) {
$class = $paged == $link ? ' class="active"' : '';
printf( '<li%s><a href="%s">%s</a></li>' . "\n", $class, esc_url( get_pagenum_link( $link ) ), $link );
}
/** Link to last page, plus ellipses if necessary */
if ( ! in_array( $max, $links ) ) {
if ( ! in_array( $max - 1, $links ) )
echo '<li>…</li>' . "\n";
$class = $paged == $max ? ' class="active"' : '';
printf( '<li%s><a href="%s">%s</a></li>' . "\n", $class, esc_url( get_pagenum_link( $max ) ), $max );
}
/** Next Post Link */
if ( get_next_posts_link() )
printf( '<li>%s</li>' . "\n", get_next_posts_link() );
echo '</ul></div>' . "\n";
}
in custom post query
<?php
global $post;
$args = array( 'posts_per_page' => 5, 'post_type'=> 'latestnews');
$myposts = get_posts( $args );
foreach( $myposts as $post ) : setup_postdata($post);
?>
<div class="page_news">
<div class="single_page_news">
<h2><?php the_title(); ?><h2>
<p><?php the_content(); ?></p>
</div>
</div>
<?php endforeach; ?>
<?php wpbeginner_numeric_posts_nav(); ?>
please help me
Your pagination function is only set up for the default main query, not for a custom query.
Also, don't use get_posts
for paginated queries. It it is nice function to use for a custom query, but it becomes a bummer to work with once you require pagination.
Rather use WP_Query
for paginated queries, it is much easier to use.
Example:
$paged = (get_query_var(‘paged’)) ? get_query_var(‘paged’) : 1;
$args = array(
'posts_per_page' => 1,
'paged' => $paged,
'post_type' => 'YOUR POST TYPE'
);
$q = new WP_Query($args);
if($q->have_post()) {
while($q->have_posts()) {
$q->the_post();
//YOUR LOOP
}
//YOUR PAGINATION
}
wp_reset_postdata();
You can have a look at the codex for extra parameters.
You now need to change every instance of $wp_query
in your pagination function to $q
for it to work.
Just a point of note, you don't have to call the $post
global
EDIT
From your comments, there is a much easier way to accomplish your goal without any custom query
This page is an archive page that is meant to display your custom post type latestnews
. You can simply just rename your archive-custom.php
to archive-latestnews.php
. See the Template Hierarchy. Just make sure has_archive
is set to true
when you register your post type
You should also never shop the main query for a custom query on any type of archive page. It is always troublesome, as you can see. So, delete your custom query and replace it with the default query
This is all you should have in your archive page
if(have_post()) {
while(have_posts()) {
the_post();
//YOUR LOOP
}
//YOUR PAGINATION
}
Just change all the instances of $q
back to $wp_query
again. Everything should work then
For extra info, you have to check out this post I've done on WPSE
这篇关于分页不适用于 wordpress 自定义帖子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!