Wordpress分页头疼



我使用标准的wordpress循环来返回帖子。我对默认WP帖子所做的唯一修改是在函数文件中将帖子重新标记为新闻。

function revcon_change_post_label() {
global $menu;
global $submenu;
$menu[5][0] = 'News';
$submenu['edit.php'][5][0] = 'News';
$submenu['edit.php'][10][0] = 'Add News';
$submenu['edit.php'][16][0] = 'News Tags';
}
function revcon_change_post_object() {
global $wp_post_types;
$labels = &$wp_post_types['post']->labels;
$labels->name = 'News';
$labels->singular_name = 'News';
$labels->add_new = 'Add News';
$labels->add_new_item = 'Add News';
$labels->edit_item = 'Edit News';
$labels->new_item = 'News';
$labels->view_item = 'View News';
$labels->search_items = 'Search News';
$labels->not_found = 'No News found';
$labels->not_found_in_trash = 'No News found in Trash';
$labels->all_items = 'All News';
$labels->menu_name = 'News';
$labels->name_admin_bar = 'News';
}
add_action( 'admin_menu', 'revcon_change_post_label' );
add_action( 'init', 'revcon_change_post_object' ); 

如果将永久链接设置为plain,则分页工作正常。但当我将永久链接更改为post name时,分页就停止了?要么按照分页链接,要么手动转到/news/page/2/,只需重新加载/news/。

如果我转到/news/page/72/pagination,它会再次工作到最后一页(83(,但向下导航到/news/page/71会再次返回/news/。

我尝试过:•剥离环路的一部分,以防有东西损坏。•禁用插件(我不能全部禁用,因为这是一个实时网站(。•使用wp_Query 创建分页循环

除了将permalink结构更改为plain之外,什么都没有起作用,因此URL将返回为siteurl.com/?p=123.

这简直把我逼疯了!有人能建议一些我没试过的东西吗?

<?php $loopcounter = 1; if (have_posts()): while (have_posts()) : the_post(); ?>
<!-- article -->
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<div class="col-1-1 mobile-col-1-1 nopad clearfix news-loop">
<div class="col-4-12 mobile-col-1-1 clearfix">  
<!-- post thumbnail -->
<?php if ( has_post_thumbnail()) :?>
<a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">
<?php the_post_thumbnail();?>
</a>
<?php endif; ?>
<!-- /post thumbnail -->
</div>
<div class="col-8-12 mobile-col-1-1 clearfix">
<!-- post title -->
<h2>
<a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a>
</h2>
<!-- /post title -->

<?php html5wp_excerpt('html5wp_custom_post') ?>
</div>
</div>
</article>
<!-- /article -->
<?php if ($loopcounter % 5 == 0):?>
<div class="col-1-1 mobile-col-1-1 nopad clearfix loopcta">
<?php get_template_part('cta2'); ?>
</div>  
<?php endif;?>
<?php $loopcounter++; endwhile; ?>
<?php get_template_part('pagination'); ?>
<?php else: ?>
<!-- article -->
<article>
<h2><?php _e( 'Sorry, nothing to display.', 'a51_blank' ); ?></h2>
</article>
<!-- /article -->
<?php endif; ?>

我还没能解决这个问题,但在url/test/下创建一个带有分页wp_query的页面模板是可行的,但当在url/news/下实现时,它又坏了。我暂时找到的解决方法是创建一个/company news/页面,并设置301将/news/重定向到/company新闻/。

我知道这并不是一个真正的解决方案,因为我还没有解决默认循环的任何问题,然而,它目前有效。

代码:

<?php 
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$news_arg = array( 'post_type' => 'post', 'posts_per_page'=> 10, 'paged' => $paged );
$news_query = new WP_Query($news_arg);
global $wp_query;
$tmp_query = $wp_query;
$wp_query = null;
$wp_query = $news_query;

$loopcounter = 1; if ($news_query->have_posts()): while ($news_query->have_posts()) : $news_query->the_post(); ?>
<!-- article -->
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<div class="col-1-1 mobile-col-1-1 nopad clearfix news-loop">
<div class="col-4-12 mobile-col-1-1 clearfix">  
<!-- post thumbnail -->
<?php if ( has_post_thumbnail()) : ?>
<a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">
<?php the_post_thumbnail(); ?>
</a>
<?php endif; ?>
<!-- /post thumbnail -->
</div>
<div class="col-8-12 mobile-col-1-1 clearfix">
<!-- post title -->
<h2>
<a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a>
</h2>
<!-- /post title -->

<?php html5wp_excerpt('html5wp_custom_post'); ?>
</div>
</div>
</article>
<!-- /article -->
<?php if ($loopcounter % 5 == 0):?>
<div class="col-1-1 mobile-col-1-1 nopad clearfix loopcta">
<?php get_template_part('cta2'); ?>
</div>  
<?php endif;?>
<?php $loopcounter++; endwhile; ?>
<style>.navigation.pagination h2{display:none;}</style>
<?php the_posts_pagination( array(
'mid_size'  => 2,
'prev_text' => __( 'Previous', 'textdomain' ),
'next_text' => __( 'Next', 'textdomain' ),
) );?>
<?php else: ?>
<!-- article -->
<article>
<h2><?php _e( 'Sorry, nothing to display.', 'a51_blank' ); ?></h2>
</article>
<!-- /article -->
<?php wp_reset_postdata(); endif; 
$wp_query = null;
$wp_query = $tmp_query;?> 

最新更新