WordPress分页不会切换页面
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了WordPress分页不会切换页面相关的知识,希望对你有一定的参考价值。
我一直在寻找这个问题的解决方案超过一个星期,我找不到其他人遇到同样的麻烦。
我正在开发一个其他人建立的自定义WP主题。有一个单页模板,我需要为其中一个辅助循环实现分页。我一直在尝试使用内置的paginate_links()
函数以及其他方法。分页链接显示,但是当我点击分页链接时,它不会在分页中转到该页面。而是重新加载原始页面(即,不是访问网站http://www.my-page/page/2/,而是重新加载网站http://www.my-page/)。
前面的dev在functions.php中使用了这个过滤器来加载正确的模板:
add_filter('single_template', create_function('$t', 'foreach( (array) get_the_category() as $cat ) { if ( file_exists(TEMPLATEPATH . "/single-{$cat->slug}.php") ) return TEMPLATEPATH . "/single-{$cat->slug}.php"; } return $t;' ));
这是我的模板文件:
<?php
/**
* Template Name: Project Template
*/
get_header('news'); ?>
<article role="main" class="projectpage">
<div class="container">
<section class="pagecontent">
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<section class="overview">
<h1><?php the_title(); ?></h1>
<div>
<?php the_content(); ?>
</div>
<div>
<?php if(get_post_meta($post->ID, 'pagelink', true)): ?>
<a href ="<?php echo get_post_meta($post->ID, 'pagelink', true); ?>" class="ctabutton2"> Read the Overview </a>
<?php endif; ?>
</div>
</div><!--end row-->
</section><!--end overview-->
<?php endwhile ?>
<?php wp_reset_postdata() ?>
<? endif ?>
<section class="related">
<div>
<h1> Related Resources </h1>
<h2> Explore our library of articles and resources </h2>
</div>
<div class="row">
<div class="col-lg-3 col-md-3 col-sm-3 col-xs-3 relatedlinks">
<section class="projectcategories">
<h3> Categories </h3>
<ul>
<?php wp_list_categories( array(
'orderby' => 'id',
'show_count' => true,
'use_desc_for_title' => false,
'child_of' => 93,
'title_li' => ' '
) ); ?>
</ul>
</section>
<section class="project-search" role="search">
<form method="get" action="<?php echo esc_url( home_url( '/' ) ); ?>">
<input type="hidden" name="cat" id="cat" value="93" />
<input type="text" size="16" name="s" placeholder="search keywords" class="search-box" />
<input type="submit" value="Go" class="go"/>
</form>
</section>
<section class="otherprojects">
<h3> Other Projects </h3>
<?php
$args = array(
'category__in' => 91,
'post__not_in' => array( $post->ID )
);
// the query
$query = new WP_Query( $args );
$temp_query = $wp_query;
$wp_query = NULL;
$wp_query = $query;
// The Loop
if ( $query->have_posts() ) : while ( $query->have_posts() ) : $query->the_post(); ?>
<a href="<?php the_permalink();?>"><?php the_title(); ?></a>
<? endwhile ;
/* Restore original Post Data */
wp_reset_postdata();
endif;
$wp_query = NULL;
$wp_query = $temp_query;
?>
</section>
</div><!--end col 1-->
<div class="col-lg-9 col-md-9 col-sm-9 col-xs-9">
<section class="articles">
<?php
// THIS IS THE SECTION WHERE I NEED THE PAGINATION
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = [
'posts_per_page' => 3,
'paged' => $paged,
'post_type' => 'post',
'order' => 'DESC',
'post__not_in' => array( $post->ID ),
'tax_query' => [
[
'taxonomy' => 'category',
'field' => 'term_id',
'terms' => '93',
],
],
];
$custom_query = new WP_Query( $args );
$temp_query = $wp_query;
$wp_query = NULL;
$wp_query = $custom_query;
if ( $custom_query->have_posts() ) {
while ( $custom_query->have_posts() ) {
$custom_query->the_post(); ?>
<div class="row">
<div class="col-md-2 col-sm-2 col-xs-2 divider">
<p class="date"><?php the_time('M j') ?></p>
</div><!--end col-->
<div class="col-md-4 col-sm-4 col-xs-4">
<div class="articleimg">
<?php if ( has_post_thumbnail()) {?>
<a href="<?php the_permalink(); ?>"><?php the_post_thumbnail('blog-thumb'); ?></a>
<?php } ?>
</div><!--end blogimg-->
</div><!--end col-->
<div class="col-md-6 col-sm-6 col-xs-6">
<div class="blogcontent">
<h3><?php the_title();?></h3>
<p><?php the_excerpt(); ?></p>
<a href="<?php the_permalink(); ?>" class="readmore"> // read more </a>
</div><!--end blogcontent-->
</div><!--end col-->
</div><!--end row-->
<?php }
}
echo paginate_links(array(
'total' => $wp_query->max_num_pages
));
$wp_query = NULL;
$wp_query = $temp_query;
wp_reset_postdata(); ?>
</section><!--end articles-->
</div><!--end col 2-->
</div> <!--end row-->
</section><!--end related-->
<!-- ANNOUNCEMENTS -->
<!--ANNOUNCEMENT SECTION -->
<!-- dynamic content --filters posts by category and only shows 'member' posts with a limit of six posts being
displayed-->
<section id="announcement-front" class="clearfix">
<div class="container">
<div>
<?php $query = new WP_Query('posts_per_page=1&category_name=advertisement');
if ($query->have_posts()) :
while ($query->have_posts()) : $query->the_post(); ?>
<a href="<?php the_permalink()?>" <?php the_content();?> </a>
<?php endwhile ?>
<? endif ?>
<?php wp_reset_postdata() ?>
</div><!--end row-->
</div><!--container-->
</section><!--end announcement-->
</section> <!--end page content -->
</div><!--end container-->
</article>
<?php get_footer(); ?>
我意识到有一整套WordPress分页教程和线程在那里,但我还没有找到一个解决这个特殊问题。
我认为你的代码应该主要起作用。你不需要担心$wp_query
的所有保存和切换 - 你尝试过这个更简单的片段吗?它在我的环境中工作正常(虽然我不得不稍微修改查询args以使其返回任何结果)。
如果这不起作用,你可以发布它生成的html吗?
<section class="articles">
<?php
// THIS IS THE SECTION WHERE I NEED THE PAGINATION
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = [
'posts_per_page' => 3,
'paged' => $paged,
'post_type' => 'post',
'order' => 'DESC',
'post__not_in' => array( $post->ID ),
'tax_query' => [
[
'taxonomy' => 'category',
'field' => 'term_id',
'terms' => '93',
],
],
];
$custom_query = new WP_Query( $args );
if ( $custom_query->have_posts() ) {
while ( $custom_query->have_posts() ) {
$custom_query->the_post(); ?>
<div class="row">
<div class="col-md-2 col-sm-2 col-xs-2 divider">
<p class="date"><?php the_time('M j') ?></p>
</div><!--end col-->
<div class="col-md-4 col-sm-4 col-xs-4">
<div class="articleimg">
<?php if ( has_post_thumbnail()) {?>
<a href="<?php the_permalink(); ?>"><?php the_post_thumbnail('blog-thumb'); ?></a>
<?php } ?>
</div><!--end blogimg-->
</div><!--end col-->
<div class="col-md-6 col-sm-6 col-xs-6">
<div class="blogcontent">
<h3><?php the_title();?></h3>
<p><?php the_excerpt(); ?></p>
<a href="<?php the_permalink(); ?>" class="readmore"> // read more </a>
</div><!--end blogcontent-->
</div><!--end col-->
</div><!--end row-->
<?php }
}
echo paginate_links(array(
'total' => $custom_query->max_num_pages
));
wp_reset_postdata(); ?>
</section><!--end articles-->
</div><!--end col 2-->
</div> <!--end row-->
</section><!--end related-->
谢谢,乔!那篇文章指出了我正确的方向。我检查了开发工具,我确实得到了301.我尝试了你指向我的文章中的代码片段,但它不太有效,所以我用Google搜索“修复与redirect_canonical的分页”和this was the first article that popped up。我从那里拿了这个函数,把它扔进了functions.php,etvoilà!它是相同的方法,但我认为,不会将自定义帖子类型传递给条件。我希望这可以帮助将来的某个人。真是太痛苦了。这也提醒我,我对WP的了解非常少,我想了解它多少!再次感谢。
这是代码:
add_filter('redirect_canonical','custom_disable_redirect_canonical');
function custom_disable_redirect_canonical($redirect_url) {
if (is_paged() && is_singular()) $redirect_url = false;
return $redirect_url;
}
以上是关于WordPress分页不会切换页面的主要内容,如果未能解决你的问题,请参考以下文章
自定义wp_query上的Wordpress分页(next_posts_link)未显示
WordPress 中自定义 wp_query 的分页需要 404 错误页面
Swiper分页器pagination多页面使用失效问题解决