dshdsh2016 2017-03-28 14:57
浏览 45

自定义帖子分页不起作用

I know there have been so many pagination questions, but I'm really stumped here. I have a "gallery" hierarchical custom post type. For the parent posts, I'm using an index template, and the child posts uses more typical single page code.

Here's some of the single-gallery.php file. I just check to see if the post has children. If it does, I want it to be an index page.

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{

After that else, it outputs all the code for the single display, which is working fine.

In content-gallery-index.php, I have.

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

Then it goes on to handle code for another, unrelated page. Lower in the template, I have the following (I included all of it by request):

<?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>";
}

}

The strange thing is that the numbered pagination is output fine, but when I click on page two, it's like the URL just gets rewritten and the page just refreshes. This same code has worked on another page, but that page is not a custom post type. I've also tried the trick of turning wp_query to null and resetting it, but then I get other errors.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥20 云服务Linux系统TCP-MSS值修改?
    • ¥20 怎么在stm32门禁成品上增加记录功能
    • ¥15 Source insight编写代码后使用CCS5.2版本import之后,代码跳到注释行里面
    • ¥50 NT4.0系统 STOP:0X0000007B
    • ¥15 想问一下stata17中这段代码哪里有问题呀
    • ¥15 flink cdc无法实时同步mysql数据
    • ¥100 有人会搭建GPT-J-6B框架吗?有偿
    • ¥15 求差集那个函数有问题,有无佬可以解决
    • ¥15 【提问】基于Invest的水源涵养
    • ¥20 微信网友居然可以通过vx号找到我绑的手机号