【问题标题】:WordPress. Why pagination doesnt work with permalinks like /%postname% but works with default URLs?WordPress。为什么分页不适用于 /%postname% 之类的永久链接,但适用于默认 URL?
【发布时间】:2014-09-19 15:25:21
【问题描述】:

我有自定义帖子类型。 永久链接看起来像 /%postname%

我的 CPT 功能:

function cpt_lyrics() {

$labels = array(
    'name'                => _x( 'Lyrics', 'Post Type General Name', 'Lyrics' ),
    'singular_name'       => _x( 'Lyrics', 'Post Type Singular Name', 'Lyrics' ),
    'menu_name'           => __( 'Lyrics', 'Lyrics' ),
    'parent_item_colon'   => __( 'Parent Lyrics:', 'Lyrics' ),
    'all_items'           => __( 'All Lyrics', 'Lyrics' ),
    'view_item'           => __( 'View Lyrics', 'Lyrics' ),
    'add_new_item'        => __( 'Add New Lyrics', 'Lyrics' ),
    'add_new'             => __( 'Add New', 'Lyrics' ),
    'edit_item'           => __( 'Edit Lyrics', 'Lyrics' ),
    'update_item'         => __( 'Update Lyrics', 'Lyrics' ),
    'search_items'        => __( 'Search Lyrics', 'Lyrics' ),
    'not_found'           => __( 'Not found', 'Lyrics' ),
    'not_found_in_trash'  => __( 'Not found in Trash', 'Lyrics' ),
);
$args = array(
    'label'               => __( 'lyrics', 'Lyrics' ),
    'description'         => __( 'Lyrics Name', 'Lyrics' ),
    'labels'              => $labels,
    'supports'            => array( 'title', 'editor', 'excerpt', 'comments', 'page-attributes', 'post-formats', ),
    'taxonomies'          => array( 'bands', 'albums' ),
    'hierarchical'        => true,
    'public'              => true,
    'show_ui'             => true,
    'show_in_menu'        => true,
    'show_in_nav_menus'   => true,
    'show_in_admin_bar'   => true,
    'menu_position'       => 7,
    'menu_icon'           => '',
    'can_export'          => true,
    'has_archive'         => true,
    'exclude_from_search' => false,
    'publicly_queryable'  => true,
    'capability_type'     => 'page',
);
register_post_type( 'lyrics', $args );
}

分页功能有效,但它显示如下链接:domain.com/page/2,我收到错误 404。 但是这些页面在 url 上可用,例如:domain.com/?page=2。 为什么会这样?

【问题讨论】:

  • 您是否更改了管理员中的永久链接结构?
  • 是的,在管理面板中,不是 .htaccess
  • 你能从你的循环中发布查询吗?
  • 当然,<?php if ( get_query_var('paged') ) $paged = get_query_var('paged'); elseif ( get_query_var('page') ) $paged = get_query_var('page'); else $paged = 1; $wp_query = new WP_Query(array( 'post_type' => 'lyrics', 'posts_per_page' => 12, 'paged' => $paged )); while ( $wp_query->have_posts() ) : $wp_query->the_post();?>
  • 不太确定该查询。试试if ( get_query_var('paged') ) { $paged = get_query_var('paged'); } elseif ( get_query_var('page') ) { $paged = get_query_var('page'); } else { $paged = 1; } query_posts('posts_per_page=12&post_type=lyrics&paged=' . $paged);

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


【解决方案1】:

你也可以使用分页插件。 https://wordpress.org/plugins/wp-paginate/ 要么 https://wordpress.org/plugins/wp-smart-pagination/ 比如更多的插件..

【讨论】:

  • 分页是一个相当简单的事情,我不建议使用插件
  • @Amit Lokulwar,我遇到了问题。不是 AdamHughes)而且,是的,它解决了 - 查看最新答案
【解决方案2】:

您需要在循环上方添加分页查询,以使分页适用于我相信的自定义帖子类型。

<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;

query_posts('posts_per_page=3&paged=' . $paged); 
?>

if ( get_query_var('paged') ) { $paged = get_query_var('paged'); } 
elseif ( get_query_var('page') ) { $paged = get_query_var('page'); } 
else { $paged = 1; }  
query_posts('posts_per_page=12&post_type=lyrics&paged=' . $paged);

【讨论】:

  • 仍然不起作用:\ 可能是用 .htaccess 或其他东西重写 url?我打破了我的头是为什么它不起作用......
【解决方案3】:

可能对某人有用...将其添加到functions.php:

function my_post_queries( $query ) { 
if (!is_admin() && $query->is_main_query()){
$taxonomies = array ('YOUR_TAXONOMY');
    if(is_tax($taxonomies) || is_home())  {         
        $query->set ('post_type', 'YOUR_POST_TYPE');
    }
  }
}
add_action( 'pre_get_posts', 'my_post_queries' );

【讨论】:

    猜你喜欢
    • 2017-09-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-18
    • 2012-09-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多