分页不适用于Wordpress

时间:2013-11-06 05:26:27

标签: php wordpress pagination

无法弄清楚为什么它会突然停止工作。该网站是:http://www.revival.tv/sermons/topical-messages/

一旦进入第二页,它就会使用存档模板:(不知道它为什么这样做,它在去年工作正常。想知道如果Wordpress更新做到了,我们只是注意到它。

我使用以下内容进行分页:

function pagination($pages = '', $range = 2) {  
     $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($pages != 1) {
         echo "<div class='pagination group'>";
         if($paged > 2 && $paged > $range+1 && $showitems < $pages) echo "<a href='".get_pagenum_link(1)."'>&laquo;</a>";
         if($paged > 1 && $showitems < $pages) echo "<a href='".get_pagenum_link($paged - 1)."'>&lsaquo;</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-page'>".$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)."'>&rsaquo;</a>";  
         if ($paged < $pages-1 &&  $paged+$range-1 < $pages && $showitems < $pages) echo "<a href='".get_pagenum_link($pages)."'>&raquo;</a>";
         echo "</div>\n";
     }
}

然后重写布道:

// Rewrite the Sermon URL
function sermon_rewrite() {
    $cpt = 'sermon';
    $tax = 'series';

    add_rewrite_rule(
        '^'.$cpt.'s/([^/]+)/page/([0-9]+)/?$',
        'index.php?post_type='.$cpt.'&'.$tax.'=$matches[1]&paged=$matches[2]',
        'top'
    );
    add_rewrite_rule(
        '^'.$cpt.'s/([^/]+)/([^/]+)/?',
        'index.php?post_type='.$cpt.'&'.$tax.'=$matches[1]&'.$cpt.'=$matches[2]',
        'top'
    );
    add_rewrite_rule(
        '^'.$cpt.'s/([^/]+)/([^/]+)/([^/]+)/?',
        'index.php?post_type='.$cpt.'&'.$tax.'=$matches[1]&'.$tax.'=$matches[2]&'.$cpt.'=$matches[3]',
        'top'
    );
}
add_action('init','sermon_rewrite');

// Update Wordpress with the rewrite
function sermon_link( $link, $post = 0 ) {
    $cpt = 'sermon';
    $tax = 'series';
    $parent = null;

    $terms = get_the_terms($post->ID, $tax);

    if( !$terms ) {
        $child = 'other';
    } else {
        $child_obj = array_pop($terms);
        $parent_obj = get_term_by('id', $child_obj->parent, $tax);
        $child = $child_obj->slug;
        if ( $parent_obj ) {
            $parent = $parent_obj->slug;
        }
    }

    if ( $post->post_type == $cpt && $terms ) {
        return home_url(user_trailingslashit($cpt.'s/'.$parent.'/'.$child.'/'.$post->post_name));
    } else if ( $post->post_type == $cpt && !$terms ) {
        return home_url(user_trailingslashit($cpt.'s/'.$child.'/'.$post->post_name));
    } else {
        return $link;
    }
}
add_filter('post_type_link', 'sermon_link', 1, 3);

1 个答案:

答案 0 :(得分:0)

我想我已经找到了问题(我只是试用并使用您提供的网址出错)。以下URL成功加载第2页。

http://www.revival.tv/sermons/topical-messages/page/2/?post_type=sermons

这让我觉得只需在index.php?post_type='.$cpt之后添加s即可。见下面的示例:

add_rewrite_rule(
    '^'.$cpt.'s/([^/]+)/page/([0-9]+)/?$',
    'index.php?post_type='.$cpt.'s&'.$tax.'=$matches[1]&paged=$matches[2]',
    'top'
);