dongren1977 2013-11-06 05:26
浏览 34
已采纳

分页不适用于Wordpress

Can't figure out why this stopped working all of a sudden. The website is: http://www.revival.tv/sermons/topical-messages/

once it goes to page two it just uses the archive template :( not sure why it is doing this, it worked fine for the last year. Wondering if a Wordpress update did it, we just noticed it.

I am using the following for the pagination:

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>
";
     }
}

and then this rewrite for the sermons:

// 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条回答 默认 最新

  • donglu7998 2013-11-06 05:39
    关注

    I think I have found the problem (I'm just trial and erroring here using your provided URL). The following URL successfully loads page 2.

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

    Which leads me to think that simply adding the s after index.php?post_type='.$cpt should do the trick. See sample below:

    add_rewrite_rule(
        '^'.$cpt.'s/([^/]+)/page/([0-9]+)/?$',
        'index.php?post_type='.$cpt.'s&'.$tax.'=$matches[1]&paged=$matches[2]',
        'top'
    );
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法