重写自定义帖子类型的URL后,分页不起作用

时间:2016-02-22 05:32:19

标签: wordpress url-rewriting pagination custom-post-type custom-taxonomy

我试过这个功能来重写自定义帖子的网址。 这是我的url重写代码,

add_filter('post_type_link', 'brand_permalink', 1, 3);
function brand_permalink($permalink, $post_id, $leavename) {

if (strpos($permalink, '%campaign-responses-category%') === FALSE) return  $permalink;

    $post = get_post($post_id);
    if (!$post) return $permalink;

    // Get taxonomy terms
    $terms = wp_get_object_terms($post->ID, 'campaign-responses-category');
    if (!is_wp_error($terms) && !empty($terms) && is_object($terms[0]))
        $taxonomy_slug = $terms[0]->slug;
    else $taxonomy_slug = 'no-brand';
    return str_replace('%campaign-responses-category%', $taxonomy_slug,  $permalink);
}

这是“分页守则”。

function wpbeginner_numeric_posts_nav() {

if( is_singular() )
    return;

global $wp_query;

/** Stop execution if there's only 1 page */
if( $wp_query->max_num_pages <= 1 )
    return;

$paged = get_query_var( 'paged' ) ? absint( get_query_var( 'paged' ) ) : 1;
$max   = intval( $wp_query->max_num_pages );

/** Add current page to the array */
if ( $paged >= 1 )
    $links[] = $paged;

/** Add the pages around the current page to the array */
if ( $paged >= 3 ) {
    $links[] = $paged - 1;
    $links[] = $paged - 2;
}

if ( ( $paged + 2 ) <= $max ) {
    $links[] = $paged + 2;
    $links[] = $paged + 1;
}

echo '<div class="pager"><div class="pages wp-compaign">' . "\n";

/** Previous Post Link */
if ( get_previous_posts_link() )
    printf( '<li style="display: inline-block;list-style: none;">%s</li>' . "\n", get_previous_posts_link() );

/** Link to first page, plus ellipses if necessary */
if ( ! in_array( 1, $links ) ) {
    $class = 1 == $paged ? ' class="active"' : '';

    printf( '<li%s style="display: inline-block;list-style: none;"><a class="page larger" href="%s">%s</a></li>' . "\n", $class, esc_url( get_pagenum_link( 1 ) ), '1' );

    if ( ! in_array( 2, $links ) )
        echo '<li style="display: inline-block;list-style: none;">…</li>';
}

/** Link to current page, plus 2 pages in either direction if necessary */
sort( $links );
foreach ( (array) $links as $link ) {
    $class = $paged == $link ? ' class="active"' : '';
    printf( '<li%s style="display: inline-block;list-style: none;"><a  class="page larger" href="%s">%s</a></li>' . "\n", $class, esc_url(  get_pagenum_link( $link ) ), $link );
 }

/** Link to last page, plus ellipses if necessary */
if ( ! in_array( $max, $links ) ) {
    if ( ! in_array( $max - 1, $links ) )
        echo '<li style="display: inline-block;list-style: none;">…</li>' .  "\n";

    $class = $paged == $max ? ' class="active"' : '';
    printf( '<li%s style="display: inline-block;list-style: none;"><a   class="page larger" href="%s">%s</a></li>' . "\n", $class, esc_url(   get_pagenum_link( $max ) ), $max );
  }

 /**    Next Post Link */
 if ( get_next_posts_link() )
    printf( '<li style="display: inline-block;list-style: none;">%s</li>' .   "\n", get_next_posts_link() );

 echo '</div></div>' . "\n";

 }

分页链接为http://example.com/site-name/campaign-responses/page/2/ 但它找不到404错误页面。 一旦我删除.htaccess并删除重写网址的功能,但仍然得到404错误。请任何人帮助我。提前谢谢。

1 个答案:

答案 0 :(得分:0)

添加重写后,您转到设置 - &gt;固定链接并单击保存修改按钮?通过这样做,.htaccess文件被重写,您的更改应该有效。