Wordpress类别分页返回404

时间:2019-09-16 23:02:41

标签: php wordpress wordpress-theming custom-wordpress-pages

我知道这可能是一个重复的问题,但是我在这里找不到的答案对我有帮助,所以我还是决定问这个问题。

我正在使用自定义帖子类型创建WP模板。我正在使用本教程构建分页https://medium.com/@bikramkc/wordpress-custom-pagination-functions-php-without-a-plugin-961bc4fb930f

我在cpt存档上运行良好, http://angelsports.com.br/custom_post_type/

但是当我尝试转到http://angelsports.com.br/custom_post_type/page/2时,它将返回404。

这是我正在使用的代码:

// archive.php

<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = [
    "post_type" => "clientes",
    "posts_per_page" => '12',
    "paged" => $paged
];
query_posts($args);
if (have_posts()) :
    ?>
<ul class="customers-section--list">
    <?php
        while (have_posts()) : the_post(); ?>
[...]
<?php
    if (function_exists('custom_pagination')) {
        custom_pagination($loop->max_num_pages, "", $paged);
    }
endif;
?>
// category.php

<?php

    $currCat = get_category(get_query_var('cat'));
    $cat_name = $currCat->name;
    $cat_id   = get_cat_ID( $cat_name );

    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    $args = [
        "post_type" => "clientes",
        "posts_per_page" => '12',
        "paged" => $paged,
        'category_name' => $cat_name
    ];
    query_posts($args);
    if (have_posts()) :
?>
<ul class="customers-section--list">
    <?php
        while (have_posts()) : the_post();
[...]
<?php
    if (function_exists('custom_pagination')) {
        custom_pagination($loop->max_num_pages, "", $paged);
    }
endif;

自定义帖子类型设置

add_action('init', 'cpt_customers');
function cpt_customers()
{
    $labels = [
        'name' => 'Clientes',
        'singular_name' => 'Cliente'
    ];

    $args = [
        'labels' => $labels,
        'supports' => ['title', 'editor', 'thumbnail'],
        'public' => true,
        'menu_icon'   => 'dashicons-groups',
        'taxonomies' => ['category'],
        'has_archive' => true,
    ];

    register_post_type('clientes', $args);
}

1 个答案:

答案 0 :(得分:1)

使用此

echo paginate_links( array(
    'current' => max( 1, $paged ),
    'total'   => $loop->max_num_pages
) );

在使用新循环之前,您将获得$ paged ...变量定义应低于 query_posts