我在Wordpress网站上建立了一个新闻部分,其中包含高级自定义字段和自定义帖子类型ui插件。在自定义新闻概述页面(不是标准的wordpress帖子)上,我想将每页的新闻数量限制为6条,并提供带有paginate\\u链接的导航。我构建了以下代码段:
<section class="news-main" role="main">
<?php
$args = array(
\'post_type\' => \'news\',
\'posts_per_page\' => \'6\'
);
$the_query = new WP_Query( $args );
$temp_query = $wp_query;
$wp_query = NULL;
$wp_query = $the_query;
$pagination = array(
\'base\' => \'%_%\',
\'format\' => \'?page=%#%\',
\'total\' => $the_query->max_num_pages,
\'prev_next\' => True,
\'prev_text\' => __( \'<< Previous\' ),
\'next_text\' => __( \'Next >>\' )
);
if ( $the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<article class="news-snippet">
<header class="news-box">
<h2 class="mus-hi slogan-head fancy"><span class="fancy-hook"><?php the_time( \'j. F Y\' ); ?></span></h2>
<a href="<?php the_permalink(); ?>"><p class="bran-hn slogan-state closure"><?php the_title(); ?></p></a>
</header>
<div class="news-wrap">
<p class="news-excerpt"><?php echo acf_excerpt( \'news_post\', 35, \' <span class="news-more-inbox">[...]</span>\' ); ?></p>
<p class="bran-hn news-more"><a href="<?php the_permalink(); ?>">More →</a></p>
</div>
</article>
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
<?php else: ?>
<p>No entries</p>
<?php endif; ?>
<nav>
<?php echo paginate_links( $pagination );
$wp_query = NULL;
$wp_query = $temp_query; ?>
</nav>
</section>
首先定义wp\\u查询的参数,然后
$the_query
在
$wp_query
变量保存到临时变量中,用NULL重置,然后设置为
$the_query
变量在最后一步中
paginate_links
已设置。然后是输出新闻帖子的循环。循环完成后,调用paginate\\u links函数,最后调用
$wp_query
已重置并写回临时
$temp_query
变量
如果我有15篇博客文章,我的输出如下:
如果我将鼠标悬停在数字3上,则slug的结尾如下所示:
现在的问题是,即使设置了上一个和下一个链接,也不会显示它们。虽然主要问题是给定的slug-如果我单击数字3,我会在404页面上着陆。wordpress不知道我被引导到哪里的“第3页”。提前谢谢Ralf
Update:
$args = array(
\'post_type\' => \'news\',
\'posts_per_page\' => \'1\'
);
$the_query = new WP_Query( $args );
$temp_query = $wp_query;
$wp_query = NULL;
$wp_query = $the_query;
$total_pages = $wp_query->max_num_pages;
if ( $total_pages > 1) {
$paged = (get_query_var(\'paged\')) ? get_query_var(\'paged\') : 1;
$current_page = new WP_Query(\'post_type=news&posts_per_page=1&paged=\' . $paged);
$pagination = array(
\'base\' => \'%_%\',
\'format\' => \'?paged=%#%\',
\'mid-size\' => 1,
\'current\' => $current_page,
\'total\' => $total_pages,
\'prev_next\' => True,
\'prev_text\' => __( \'<< Previous\' ),
\'next_text\' => __( \'Next >>\' )
);
}
更新2:
好了,对代码的第一部分做了以下修改,我让导航正常工作了
$args = array(
\'post_type\' => \'news\',
\'posts_per_page\' => \'3\',
\'paged\' => get_query_var( \'paged\' )
);
$the_query = new WP_Query( $args );
$temp_query = $wp_query;
$wp_query = NULL;
$wp_query = $the_query;
$total_pages = $wp_query->max_num_pages;
if ( $total_pages > 1) {
$the_paged = (get_query_var(\'paged\')) ? get_query_var(\'paged\') : 1;
$pagination = array(
\'base\' => @add_query_arg(\'paged\',\'%#%\'),
\'format\' => \'?paged=%#%\',
\'mid-size\' => 1,
\'current\' => $the_paged,
\'total\' => $total_pages,
\'prev_next\' => True,
\'prev_text\' => __( \'<< Previous\' ),
\'next_text\' => __( \'Next >>\' )
);
}
我剩下的唯一一个问题(唯一一个仍然不正常的问题)是第一个链接是否有可能是link而不是link/?分页=1