自定义帖子分页不起作用

时间:2017-03-28 14:57:45

标签: php wordpress pagination custom-post-type

我知道有很多分页问题,​​但我真的很难过。我有一个“图库”分层自定义帖子类型。对于父帖子,我使用的是索引模板,子帖子使用更典型的单页代码。

这是一些single-gallery.php文件。我只是检查帖子是否有孩子。如果是,我希望它是一个索引页面。

get_header(); 
$template = get_template_directory_uri();

//If it's an index page, redirect it
$args = array(
    'post_parent' => $post->ID,
    'post_type'   => 'gallery', 
    'numberposts' => -1,
    'post_status' => 'publish' 
);
$children = get_children( $args );

if ($children){
    get_template_part('template-parts/content', 'gallery-index');
    get_footer();
} else{

之后,它会输出单个显示的所有代码,工作正常。

在content-gallery-index.php中,我有。

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

然后它继续处理另一个不相关的页面的代码。在模板中较低,我有以下内容(我根据要求包含了所有内容):

<?php elseif (is_page(343) || $post_type == 'gallery' ):

    $post_parent = 343;
    $type = 'page';
    if ($post_type == 'gallery'){
        $post_parent = $post->ID;
        $type = 'gallery';
    }
    $args = array(
        'post_type' => $type,
        'post_parent' => $post_parent,
        'posts_per_page' => 6,
        'paged' => $paged,
    );
    $query = new WP_Query($args);
    if ($query->have_posts()): ?>
        <div class="box-steps">
            <?php while($query->have_posts()): $query->the_post(); 
                $image = get_the_post_thumbnail();
                $permalink = get_the_permalink();
                if (!get_field('dont_include_in_services')): ?>

                    <section class="box-step">
                        <div class="box-step-img-wrapper">
                            <a href="<?php echo $permalink; ?>">
                                <?php the_post_thumbnail(); ?>
                            </a>
                        </div>
                        <div class="box-step-content-wrapper">
                            <h2 class="box-step-title our-services-title header-font"><?php echo get_the_title(); ?></h2>
                            <?php if ($post_type == 'gallery'): ?>
                                <p class="box-step-content"><?php echo get_the_content($post->ID); ?></p>
                            <?php else: ?>
                                <p class="box-step-content"><?php echo esc_html(get_field('preview_text')); ?></p>
                                <a class="box-step-link block bold" href="<?php echo $permalink; ?>">
                                    READ MORE
                                </a>
                            <?php endif; ?>
                        </div>
                    </section>

                <?php endif; ?>

            <?php endwhile; ?>
        </div>  
    <?php endif; wp_reset_postdata(); ?>
    <!-- pagination here -->
    <?php
        custom_pagination($query->max_num_pages,"",$paged);
    ?>

<?php endif; ?>

的functions.php

function custom_pagination($numpages = '', $pagerange = '', $paged='') {

if (empty($pagerange)) {
    $pagerange = 2;
}

global $paged;
if (empty($paged)) {
    $paged = 1;
}
if ($numpages == '') {
    global $wp_query;
    $numpages = $wp_query->max_num_pages;
    if(!$numpages) {
        $numpages = 1;
    }
}

$prev_arrow = is_rtl() ? '&raquo;' : '&laquo;';
$next_arrow = is_rtl() ? '&laquo;' : '&raquo;';

$pagination_args = array(
    'base'            => get_pagenum_link(1) . '%_%',
    'format'          => 'page/%#%',
    'total'           => $numpages,
    'current'         => $paged,
    'show_all'        => False,
    'end_size'        => 1,
    'mid_size'        => $pagerange,
    'prev_next'       => True,
    'prev_text'       => __('&laquo;'),
    'next_text'       => __('&raquo;'),
    'type'            => 'list',
    'add_args'        => false,
    'add_fragment'    => '',
    'prev_text'     => $prev_arrow,
    'next_text'     => $next_arrow,
);


$paginate_links = paginate_links($pagination_args);

if ($paginate_links) {
    echo "<nav class='custom-pagination'>";
    //echo "<span class='page-numbers page-num'>Page " . $paged . " of " . $numpages . "</span> ";
    echo $paginate_links;
    echo "</nav>";
}

}

奇怪的是,编号分页输出正常,但是当我点击第二页时,就像URL被重写一样,页面只是刷新。此相同的代码已在另一个页面上工作,但该页面不是自定义的帖子类型。我也尝试过将wp_query转为null并重置它的技巧,但后来我遇到了其他错误。

0 个答案:

没有答案