Home  >  Article  >  Backend Development  >  WordPress 在首页使用 query_posts() 分页 404?

WordPress 在首页使用 query_posts() 分页 404?

WBOY
WBOYOriginal
2016-06-06 20:23:031695browse

小弟在开发时遇到个问题,思路大概是这样的:

  • 设置四种文章类型('post','detail','jingyan','haitao),相当于与后台的「文章」同级。

  • 在首页调取四种文章类型中的所有内容,如下:

<code class="php"><?php $paged = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1;
    $sticky = get_option( 'sticky_posts' );
    $notcat = explode(',',zm_get_option('not_cat_n'));
    $args = array(
        'category__not_in' => $notcat,
        'post__not_in' => $sticky,
        'post_type'=>array( 'post','detail','jingyan','haitao'),
        'posts_per_page' => 15,
        'paged' => $paged
    );
    query_posts( $args );
?>
<?php if ( have_posts() ) : ?>
<?php while ( have_posts() ) : the_post(); ?>
...
<?php endwhile; pagenavi(); endif; wp_reset_query(); 
?></code>
  • Functions.php 加了这些:

<code class="php"><?php add_filter('pre_get_posts', 'query_post_type');
function query_post_type($query) {
  if(is_category() || is_tag()) {
    $post_type = get_query_var('post_type');
    if($post_type)
        $post_type = $post_type;
    else
        $post_type = array( 'post','detail','jingyan','haitao');
        $query->set('post_type',$post_type);
        $query->set('posts_per_page', 15);
    return $query;
    }
}</code>

问题来了: 按照这样的方法输出内容后,分页仅按照文章数显示。例如,「Post」下有 60 篇文章,「detail」、「jingyan」、「haitao」各 10 篇。按照想法应分 (60+10*3)/15 = 6 页。然而实际情况为翻至 60/15 = 4 页结束后,第 5、6 页均显示 404.

Copy 别人的 pagenavi()

<code class="php"><?php // 分页显示
function pagenavi($before = '', $after = '') {
    global $wpdb, $wp_query,$pagenavi_options;
    if (!is_single()) {
        $request = $wp_query->request;
        $posts_per_page = intval(get_query_var('posts_per_page'));
        $paged = intval(get_query_var('paged'));
        $numposts = $wp_query->found_posts;
        $max_page = $wp_query->max_num_pages;
        if(empty($paged) || $paged == 0){
            $paged = 1;
        }
        $pages_to_show = intval($pagenavi_options['num_pages']);
        $pages_to_show = intval(4);
        $pages_to_show_minus_1 = $pages_to_show-1;
        $half_page_start = floor($pages_to_show_minus_1/2);
        $half_page_end = ceil($pages_to_show_minus_1/2);
        $start_page = $paged - $half_page_start;
        if($start_page  $max_page) {
            $start_page = $max_page - $pages_to_show_minus_1;
            $end_page = $max_page;
        }
        if($start_page  1 || intval($pagenavi_options['always_show']) == 1) {
            $pages_text = str_replace("%CURRENT_PAGE%", number_format_i18n($paged), "共 %TOTAL_PAGES% 页");
            $pages_text = str_replace("%TOTAL_PAGES%", number_format_i18n($max_page), $pages_text);
            echo $before.'<div class="pagenavi">'."\n";
            if(!empty($pages_text)) {
                echo '<span class="pages">'.$pages_text.'</span>';
            }                   
            if ($start_page >= 2 && $pages_to_show '.$first_page_text.'';
                echo '<span class="extend">...</span>';
            }
            previous_posts_link("«");
            for($i = $start_page; $i  '.$current_page_text.'';
                } else {
                    $page_text = str_replace("%PAGE_NUMBER%", number_format_i18n($i), "%PAGE_NUMBER%");
                    echo '<a href="'.esc_url(get_pagenum_link(%24i)).'" title="'.$page_text.'">'.$page_text.'</a>';
                }
            }
            next_posts_link("»", $max_page);
            if ($end_page '."...".'';
                $last_page_text = str_replace("%TOTAL_PAGES%", number_format_i18n($max_page), "最后 »");
                echo '<a href="'.esc_url(get_pagenum_link(%24max_page)).'" title="'.$last_page_text.'">最后 »</a>';
            }
            echo '</div>'.$after."\n";
        }
    }
}
 
?></code>

回复内容:

小弟在开发时遇到个问题,思路大概是这样的:

  • 设置四种文章类型('post','detail','jingyan','haitao),相当于与后台的「文章」同级。

  • 在首页调取四种文章类型中的所有内容,如下:

<code class="php"><?php $paged = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1;
    $sticky = get_option( 'sticky_posts' );
    $notcat = explode(',',zm_get_option('not_cat_n'));
    $args = array(
        'category__not_in' => $notcat,
        'post__not_in' => $sticky,
        'post_type'=>array( 'post','detail','jingyan','haitao'),
        'posts_per_page' => 15,
        'paged' => $paged
    );
    query_posts( $args );
?>
<?php if ( have_posts() ) : ?>
<?php while ( have_posts() ) : the_post(); ?>
...
<?php endwhile; pagenavi(); endif; wp_reset_query(); 
?></code>
  • Functions.php 加了这些:

<code class="php"><?php add_filter('pre_get_posts', 'query_post_type');
function query_post_type($query) {
  if(is_category() || is_tag()) {
    $post_type = get_query_var('post_type');
    if($post_type)
        $post_type = $post_type;
    else
        $post_type = array( 'post','detail','jingyan','haitao');
        $query->set('post_type',$post_type);
        $query->set('posts_per_page', 15);
    return $query;
    }
}</code>

问题来了: 按照这样的方法输出内容后,分页仅按照文章数显示。例如,「Post」下有 60 篇文章,「detail」、「jingyan」、「haitao」各 10 篇。按照想法应分 (60+10*3)/15 = 6 页。然而实际情况为翻至 60/15 = 4 页结束后,第 5、6 页均显示 404.

Copy 别人的 pagenavi()

<code class="php"><?php // 分页显示
function pagenavi($before = '', $after = '') {
    global $wpdb, $wp_query,$pagenavi_options;
    if (!is_single()) {
        $request = $wp_query->request;
        $posts_per_page = intval(get_query_var('posts_per_page'));
        $paged = intval(get_query_var('paged'));
        $numposts = $wp_query->found_posts;
        $max_page = $wp_query->max_num_pages;
        if(empty($paged) || $paged == 0){
            $paged = 1;
        }
        $pages_to_show = intval($pagenavi_options['num_pages']);
        $pages_to_show = intval(4);
        $pages_to_show_minus_1 = $pages_to_show-1;
        $half_page_start = floor($pages_to_show_minus_1/2);
        $half_page_end = ceil($pages_to_show_minus_1/2);
        $start_page = $paged - $half_page_start;
        if($start_page  $max_page) {
            $start_page = $max_page - $pages_to_show_minus_1;
            $end_page = $max_page;
        }
        if($start_page  1 || intval($pagenavi_options['always_show']) == 1) {
            $pages_text = str_replace("%CURRENT_PAGE%", number_format_i18n($paged), "共 %TOTAL_PAGES% 页");
            $pages_text = str_replace("%TOTAL_PAGES%", number_format_i18n($max_page), $pages_text);
            echo $before.'<div class="pagenavi">'."\n";
            if(!empty($pages_text)) {
                echo '<span class="pages">'.$pages_text.'</span>';
            }                   
            if ($start_page >= 2 && $pages_to_show '.$first_page_text.'';
                echo '<span class="extend">...</span>';
            }
            previous_posts_link("«");
            for($i = $start_page; $i  '.$current_page_text.'';
                } else {
                    $page_text = str_replace("%PAGE_NUMBER%", number_format_i18n($i), "%PAGE_NUMBER%");
                    echo '<a href="'.esc_url(get_pagenum_link(%24i)).'" title="'.$page_text.'">'.$page_text.'</a>';
                }
            }
            next_posts_link("»", $max_page);
            if ($end_page '."...".'';
                $last_page_text = str_replace("%TOTAL_PAGES%", number_format_i18n($max_page), "最后 »");
                echo '<a href="'.esc_url(get_pagenum_link(%24max_page)).'" title="'.$last_page_text.'">最后 »</a>';
            }
            echo '</div>'.$after."\n";
        }
    }
}
 
?></code>
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn