【问题标题】:Wordpress paginate_links() function, 404s and troubleWordpress paginate_links() 函数,404s 和麻烦
【发布时间】:2013-03-05 15:54:47
【问题描述】:

这是一个可怕的、严重的、浪费时间的(两周)可怕的可怕问题。

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

paginate_links() 正在发布这样的链接:

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

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

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

它工作正常。

所以我的问题简化为:如果我必须让那个函数正确输出“page”变量,那是怎么做的?

pagedpaged 都被 WordPress 使用。反过来说,如果这是更好的做法,我如何让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();

}

【问题讨论】:

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


    【解决方案1】:

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

    这是因为,显然,您不能拥有与自定义帖子类型同名的 page-slug。

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

    【讨论】:

      【解决方案2】:

      根据您的 paginate_links() 参数,我会确保您的格式设置正确。 '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);
      

      【讨论】:

      • 我更新了原帖。不知何故,有些东西被劫持给了我一个 404。非常感谢您的回复。 wordpress.org 似乎忽略了有关此主题的大多数帮助请求。
      • 所以我在这里仍然很黑暗。我的查询/分页链接没有任何问题。我错过了什么吗?
      • 我能想到的唯一另一件事就是展示您的网站。目前我在你的代码中唯一没有看到的是全局 $wp_query;但这不应该影响您遇到的格式问题。你能展示代码的大图吗?我想不出太多会影响它的东西,但可能还有其他可能导致问题的东西。
      • 感谢您对此进行检查。我没有发布大量代码的所有内容,而是认为下一个可能的问题领域是重写规则,我已经设置了自定义帖子类型。如果您可以验证这里没有问题,那么我可能只是一个人!我刚刚在开篇文章中发布了下一组代码。
      • 这很有趣 - 对于我的自定义分类页面 PAGINATION WORKS!是否有可能我以某种方式破坏了除分类页面之外的任何地方的自定义帖子类型分页?
      【解决方案3】:

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

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

      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。如果将来有人知道更好的解决方案,我很想知道一个!

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-02-09
        • 2018-05-31
        • 1970-01-01
        • 2011-12-23
        相关资源
        最近更新 更多