簡體   English   中英

Wordpress分頁錯誤與查詢字符串

[英]Wordpress pagination error with query strings

我有一個帶有自定義WP查詢的頁面,用於輸出與獎勵帖子類型相關的不同帖子。

$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$args = array(
    'posts_per_page'        => 2,
    'ignore_sticky_posts'   => 1,
    'post_type'             => 'award',
    'post_status'           => 'publish',
    'orderby'               => 'date',
    'order'                 => 'DESC',
    's'                     => $_GET['keyword'],
    'paged'                 => $paged;
);

我正在使用默認的wordpress分頁功能

paginate_links( array(
    'base'         => str_replace( 999999999, '%#%', esc_url( get_pagenum_link( 999999999 ) ) ),
    'total'        => $query->max_num_pages,
    'current'      => $paged,
    'format'       => '?paged=%#%',
    'show_all'     => false,
    'type'         => 'plain',
    'mid_size'     => 3,
    'prev_next'    => false,
    'add_args'     => false,
    'add_fragment' => '',
));

如果我提交這個url awards/?keyword=test ,我會得到幾個頁碼的相關結果。 如果我點擊分頁上的第2頁,頁面會重定向到此網址awards/page/2/?keyword=test ,我找不到頁面錯誤! 如果我訪問awards/page/2/也會遇到同樣的錯誤! 我究竟做錯了什么?

如果我可以在URL中保留我的查詢字符串,例如這樣的awards/?page=2&keyword=test那也是很好的。

UPDATE

如果刪除獎勵自定義帖子類型但我將帖子保留在數據庫中,則過濾器的分頁可以正常工作。

此外,WP查詢位於名為“獎賞”的頁面中,其中包含一系列獎賞。

我的獎勵自定義帖子類型:

$cpt = array(
      'singular' => 'Article',
      'plural'   => 'Awards & Press',
      'type'     => 'award',
      'slug'     => 'awards'
  );

  register_post_type( $cpt['type'],
    array(
      'labels'             => array(
        'name'               => sprintf( '%s', $cpt['plural'] ),
        'singular_name'      => sprintf( '%s', $cpt['singular'] ),
        'add_new'            => 'Add New',
        'add_new_item'       => sprintf( 'Add New %s', $cpt['singular'] ),
        'edit'               => 'Edit',
        'edit_item'          => sprintf( 'Edit %s', $cpt['singular'] ),
        'new_item'           => sprintf( 'New %s', $cpt['singular'] ),
        'all_items'          => sprintf( 'All %s', $cpt['plural'] ),
        'view'               => sprintf( 'View %s', $cpt['singular'] ),
        'search_items'       => sprintf( 'Search %s', $cpt['plural'] ),
        'not_found'          => sprintf( 'No %s Found', $cpt['plural'] ),
        'not_found_in_trash' => sprintf( 'No %s Found In Trash', $cpt['plural'] ),
        'parent_item_colon'  => ''
      ),
      'description'        => 'Create a new item',
      'public'             => true,
      'show_ui'            => true,
      'show_in_menu'       => true,
      'publicly_queryable' => true,
      'has_archive'        => false,
      'rewrite'            => array(
        'slug' => $cpt['slug'],
        'with_front' => false,
      ),
      'menu_position'      => 5,
      'show_in_nav_menus'  => true,
      'menu_icon'          => 'dashicons-admin-post',
      'hierarchical'       => false,
      'query_var'          => true,
      'supports'           => array(
        'title',
        'editor',
        'excerpt',
        'thumbnail',
      )
    )
  );

另一個更新

如果我改變:

'rewrite'            => array(
            'slug' => $cpt['slug'],
            'with_front' => false,
          ),

'rewrite'            => false,

過濾器的分頁工作,但我失去了漂亮的網址awards/post-name/它成為awards/?award=post-name

我需要對過濾器進行分頁,同時保持帖子的漂亮網址。

我相信我找到了答案,但如果其他人能證實這是最好的解決方案,我會很高興:

add_action('init', function() {
  add_rewrite_rule('(.?.+?)/page/?([0-9]{1,})/?$', 'index.php?pagename=$matches[1]&paged=$matches[2]', 'top');
});

我將上面的代碼添加到functions.php文件中。

解決方案來自這里: https//www.grzegorowski.com/wordpress-rewrite-rules/

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM