Wordpress paginate_links()函数,404s和麻烦

时间:2013-03-17 05:05:58

标签: wordpress pagination http-status-code-404

这是一个可怕的,严重的,时间偷窃(两周)可怕的可怕问题。

它可能是重写,我使用自定义帖子,但在剥离了这么多(并减少了我的主题的一些功能)后,我把问题减少到了:

paginate_links()正在发布如下链接:

?s=cute&post_type=image&paged=2

当我将浏览器栏中的变量更改为page = 2(删除'd')时。

?s=cute&post_type=image&page=2 

它正常工作。

所以我的问题简化为:如果我必须正确输出“页面”变量,那该怎么办?

WordPress使用

pagedpaged。相反,如果这是更好的做法,我该如何识别paged

就我所知,这表明我的主题有一些更深层次的问题,但对于我的生活,我看不出我怎么会出错!

编辑:

这是我正在使用的代码:

if ( get_query_var('paged') )

        $paged = get_query_var('paged');

    elseif ( get_query_var('page') )

        $paged = get_query_var('page');

    else
        $paged = 1;     

    $big = 999999999; // need an unlikely integer

    $stuff_to_echo =  paginate_links( array(
        'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
        'format' => '?page=%#%',
        'current' => max( 1, $paged ),
        'total' => $wp_query->max_num_pages,
        'type' => 'array'
    ) );

编辑2 - 如果上面是好的,这是问题可能发生的另一个可能的区域。我将发布自定义帖子类型和重写规则的创建 -

//image custom post type

add_action( 'init', 'symbiostock_image_manager_register' );

function symbiostock_image_manager_register( )
{
    //creating custom post type for image

    $labels = array(
         'name' => 'Symbiostock Images',
        'singular_name' => 'Image',
        'add_new' => 'New Image',
        'add_new_item' => 'Add New Image',
        'edit_item' => 'Edit Image',
        'new_item' => 'New Image',
        'all_items' => 'All Images',
        'view_item' => 'View Image',
        'search_items' => 'Search Images',
        'not_found' => 'No image found',
        'not_found_in_trash' => 'No images found in Trash',
        'parent_item_colon' => '',
        'menu_name' => 'RF Images' 
    );

    $args = array(

         'labels' => $labels,
        'singular_label' => __( 'Image' ),
        'description' => 'Image Listings',
        'menu_position' => 100,
        'menu_icon' => symbiostock_IMGDIR . '/symbiostock_icon2.png',
        'public' => true,
        'publicly_queryable' => true,
        'show_ui' => true,
        'capability_type' => 'post',
        'hierarchical' => true,
        'has_archive' => true,
        'exclude_from_search' => false,
        'supports' => array(
             'title',
            'editor',
            'thumbnail' 
        ),
        'rewrite' => array(
             'slug' => 'image',
            'with_front' => false 
        ) 

    );


    register_post_type( 'image', $args );

    register_taxonomy( 'image-type', array(
         'image' 
    ), array(
         'hierarchical' => true,
        'label' => 'Image Categories',
        'singular_label' => 'Image Type',
        'rewrite' => true,
        'exclude_from_search' =>false,
        'public' => true, 
        'slug' => 'image-type' 
    ) );

    register_taxonomy( 'image-tags', array(
         'image' 
    ), array(
         'hierarchical' => false,
         'rewrite' => true,
         'query_var' => true,
        'singular_label' => 'Image Keyword',

        'hierarchical'            => false,
        'labels'                  => $labels,
        'show_ui'                 => true,
        'show_admin_column'       => true,
        'update_count_callback'   => '_update_post_term_count',


        'slug' => 'image-tag',               
        'labels' => array(
            'name' => _x( 'Image Keywords', 'taxonomy general name' ),
            'singular_name' => _x( 'Keywords', 'taxonomy singular name' ),
            'search_items' =>  __( 'Search Images' ),
            'all_items' => __( 'All Image Keywords' ),
            'edit_item' => __( 'Edit Image Keyword' ),
            'update_item' => __( 'Update Image Keyword' ),
            'add_new_item' => __( 'Add New Image Keyword' ),
            'new_item_name' => __( 'New Keyword Name' ),
            'menu_name' => __( 'Image Keywords' ),
            ),
        'rewrite' => array(
            'slug' => 'search-images', // This controls the base slug that will display before each term
            'with_front' => false, 
            'hierarchical' => false 
        ),
    ) );

}

add_action( 'admin_init', 'symbiostock_image_directory' );

function symbiostock_image_directory( )
{
    add_meta_box( 'symbiostock-image-meta', 'Symbiostock Image Info', 'symbiostock_image_manager_meta_options', 'image', 'normal', 'high' );

}

以下是重写规则 -

add_action( 'init', 'symbiostock_rewrite' );

function symbiostock_rewrite( )
{   

    global $wp_rewrite;
    $wp_rewrite->add_permastruct('typename','typename/%year%%postname%/' , true , 1);
    add_rewrite_rule('typename/([0-9]{4})/(.+)/?$','index.php?typename=$matches[2]', 'top');
    $wp_rewrite->flush_rules();

}

3 个答案:

答案 0 :(得分:1)

我知道这已经过时了,但是我遇到了同样的问题并通过更改导致404的页面的永久链接来解决它。

这是因为,显然,您不能拥有与自定义帖子类型同名的页面标题。

所有归功于Ryan S.分享原始解决方案:http://www.sutanaryan.com/2013/09/404-error-in-custom-post-type-pagination-wordpress/

答案 1 :(得分:0)

在你的paginate_links()的args下,我会确保你正确设置你的格式。 'paged'通常用于获取用户所在的“当前页面”(参考:https://codex.wordpress.org/Pagination#Adding_the_.22paged.22_parameter_to_a_query

'format' => '?page=%#%'

听起来你把它设置为'?paged =%#%'

这是一个完整的例子

global $wp_query;
$args = array(
    'format'    => '?page=%#%',
    'current'   => max( 1, get_query_var('paged') ),
    'total'     => $wp_query->max_num_pages
);
paginate_links($args);

答案 2 :(得分:0)

它可能不是最优雅的解决方案,但在这种情况下是合适的。

如果有人正在搜索此帖子类型,我只需设置重定向到分类页面。

add_action( 'parse_query', 'image_search_redirect' );

function image_search_redirect( $query ) {
    if ( ( is_search() && get_query_var( 'post_type' ) == 'image' ) ) {

        wp_redirect(home_url("?image-tags=") . urlencode(get_query_var('s')));

        exit(); 
    }
}

这不是一个创造性解决方案的解决方案。我没有理由在“paged =”变量上找到404。如果一个未来的人知道一个更好的解决方案,我很想知道一个!

相关问题