使用自定义帖子类型和分类法的分页返回404 [英] Pagination with Custom Post Type and Taxonomies return 404

查看:82
本文介绍了使用自定义帖子类型和分类法的分页返回404的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

自上周以来,我一直在尝试解决此问题,但我无法完成.问题本身听起来很简单,当我不处理分类法时,我在WordPress上的分页就可以正常工作,例如:www.example.com/post_type/2,但是如果我尝试使用分类法,则无法超越第1页(www.example.com/taxonomy/term/2).

I've been trying to fix this issue since last week but I can't get it done. The problem itself sounds quite simple, my pagination on WordPress works just fine when it's not dealing with taxonomies, for example: www.example.com/post_type/2 but if I try to use a taxonomy, I can't go past page 1 (www.example.com/taxonomy/term/2).

Custom Post Type = noticias and Taxonomy = assunto,所以我认为这里的名称不是冲突的因素.

Custom Post Type = noticias and Taxonomy = assunto, so I don't think the name is a conflicting factor here.

$custom_query_args['paged'] = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1;
$args = array( 'post_type' => 'noticias', 'paged' => $custom_query_args['paged'], 'posts_per_page' => 5);
$loop = new WP_Query( $args );

$temp_query = $wp_query;
$wp_query   = NULL;
$wp_query   = $loop;

while ( $loop->have_posts() ) : $loop->the_post();
    <!-- Content here -->
endwhile;  

wp_reset_postdata();

if (function_exists("pagination")) {
    pagination($wp_query->max_num_pages);
}

$wp_query = $temp_query;

我正在使用atm的功能:

The function that I'm using atm:

function pagination($pages = '', $range = 4) {  
        $showitems = ($range * 2)+1;  

        global $paged;
        if(empty($paged)) $paged = 1;

        if($pages == '') {
            global $wp_query;
            $pages = $wp_query->max_num_pages;

            if(!$pages) {
                $pages = 1;
            }
        }   

        if(1 != $pages) {
            echo "<div class=\"pagination\"><span>Página ".$paged." de ".$pages."</span>";
            if($paged > 2 && $paged > $range+1 && $showitems < $pages) echo "<a href='".get_pagenum_link(1)."'>&laquo; Primeira</a>";
            if($paged > 1 && $showitems < $pages) echo "<a href='".get_pagenum_link($paged - 1)."'>&lsaquo; Anterior</a>";

            for ($i=1; $i <= $pages; $i++) {
                if (1 != $pages &&( !($i >= $paged+$range+1 || $i <= $paged-$range-1) || $pages <= $showitems )) {
                    echo ($paged == $i)? "<span class=\"current\">".$i."</span>":"<a href='".get_pagenum_link($i)."' class=\"inactive\">".$i."</a>";
                }
            }

            if ($paged < $pages && $showitems < $pages) echo "<a href=\"".get_pagenum_link($paged + 1)."\">Próxima &rsaquo;</a>";  
            if ($paged < $pages-1 &&  $paged+$range-1 < $pages && $showitems < $pages) echo "<a href='".get_pagenum_link($pages)."'>Última &raquo;</a>";
            echo "</div>\n";
        }
    }

推荐答案

好的,所以我完成了.我不知道这是否是解决问题的最佳方法,但这是我发现的唯一方法.这个答案是jcarroll780对相同问题进行一些修改后的答案的一个分支".

Ok, so I got it done. I don't know if this is the best way to solve the problem but it was the only one that I found. This answer is a "fork" of jcarroll780's answer on the same problem with some modifications.

首先,我将这些功能添加到我的functions.php中:

First I added these functions into my functions.php:

$option_posts_per_page = get_option( 'posts_per_page' );
add_action( 'init', 'my_modify_posts_per_page', 0);

function my_modify_posts_per_page() {
    add_filter( 'option_posts_per_page', 'my_option_posts_per_page' );
}

function my_option_posts_per_page( $value ) {
    global $option_posts_per_page;
    if ( is_tax( 'assunto') ) {
        return 5;
    } else {
        return $option_posts_per_page;
    }
}

然后,我使用以下循环显示正确的分页(taxonomy-assunto.php):

Then I used the following loop to display the correct pagination (taxonomy-assunto.php):

$term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) );
$paged = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1;
query_posts(array_merge( $wp_query->query, array('post_type'=>'noticias', 'posts_per_page' => 5, 'paged'=>$paged ) ) );

if (have_posts()) : while ( have_posts() ) : the_post();
    // Content Here
endwhile; endif;

if ( function_exists( 'wp_pagenavi' ) ) {
    wp_pagenavi();
}

对我来说效果很好,我希望这也能回答您的问题.

It worked pretty well for me, I hope this answers your questions aswell.

这篇关于使用自定义帖子类型和分类法的分页返回404的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆