分页不适用于自定义帖子类型

问题描述 投票:0回答:2

分页模板部分包括具有样式的通用分页功能。该模板部分可用于archive.php(其用于“单个”,您知道默认的wp文件),但不适用于自定义帖子类型。

为什么?如何解决?

Lorem Ipsum只是印刷和排版的伪文本行业。 Lorem Ipsum一直是业界标准的伪文本从1500年代开始,当一个未知的打印机拿起一个把它打乱成一本样本。它不仅存活了下来五个世纪以来,也实现了电子排版的飞跃,基本上保持不变。它在1960年代随着释放含有Lorem Ipsum通道的Letraset床单,以及最近使用桌面发布软件(例如Aldus PageMaker)包括Lorem Ipsum的版本。

<?php get_header(); ?>



<main role="main">
    <!-- section -->
    <?php get_template_part( 'breadcrumb' );?>

    <!-- Inner Pages Main Section -->
    <section class="ulockd-service-details">
        <div class="container">
            <div class="col-md-12">
                <div class="row">

                    <?php

                    /**
                     * Setup query to show the ‘services’ post type with ‘8’ posts.
                     * Output the title with an excerpt.
                     */
                    $args = array(
                        'post_type' => 'team',
                        'post_status' => 'publish',
                        'posts_per_page' => 1,
                    );

                    $loop = new WP_Query( $args );

                    if (have_posts()): while ( $loop->have_posts() ) : $loop->the_post();

                    ?>

                    <?php //if (have_posts()): while (have_posts()) : the_post(); ?>


                        <?php

                        if ( $thumbnail_id = get_post_thumbnail_id() ) {
                            if ( $image_src = wp_get_attachment_image_src( $thumbnail_id, 'normal-bg' ) )
                                ?>

                                <div class="col-md-12 ulockd-mrgn1210">
                                <div class="ulockd-project-sm-thumb">
                                <img class="img-responsive img-whp" src="<?php printf( '%s', esc_url($image_src[0]) ); ?>" alt="">
                            </div>
                            </div>

                            <?php

                        }

                        ?>





                        <div class="col-md-12 ulockd-mrgn1210">
                            <article class="ulockd-pd-content">
                                <div class="ulockd-bp-date">
                                    <ul class="list-inline">
                                        <li class="ulockd-bp-date-innner">On <a href="#"><span class="text-thm2"><?php the_time('j'); ?></span> / <?php the_time('F Y') ?></a></li>
                                        <li class="ulockd-bp-comment"><a href="#"><span class="flaticon-nurse-head text-thm1"></span> <?php the_author_posts_link(); ?></a></li>
                                        <li class="ulockd-bp-comment"><a href="#"><span class="flaticon-chat text-thm1"></span> <?php if (comments_open( get_the_ID() ) ) comments_popup_link( __( 'Leave your thoughts', 'html5blank' ), __( '1 Comment', 'html5blank' ), __( '% Comments', 'html5blank' )); ?></a></li>
                                        <li class="ulockd-bp-comment"><a href="#"><span class="flaticon-black-check-box text-thm1"></span> <?php the_category(); ?></a></li>
                                    </ul>
                                </div>
                                <h3><?php the_title(); ?> </h3>
                                <p class="project-dp-one"><?php html5wp_excerpt('html5wp_index'); // Build your custom callback length in functions.php ?></p>
                                <a class="btn btn-lg ulockd-btn-thm2" href="<?php the_permalink(); ?>"> Read More</a>
                            </article>
                        </div>

                        <?php get_template_part('pagination'); ?>
                    <?php endwhile; ?>

                    <?php else: ?>
                        <article>
                            <h2><?php _e( 'Sorry, nothing to display.', 'html5blank' ); ?></h2>
                        </article>
                    <?php endif; ?>

                </div></div></div></section>



    <?php get_footer(); ?>
</main>
pagination wordpress-theming custom-post-type
2个回答
0
投票

首先,您不需要在while循环中包含该模板。错了。

然后,如果要为team帖子类型创建一个存档页面,则需要在'has_archive' => true函数参数中提供register_post_type()

[此外,如果需要,请考虑更改存档页面默认标记。如果这样做,则需要打开Settings > Permalinks来重置永久链接结构。

然后,您可以在整个团队存档页面中使用标准archive.php,对于循环内的一个帖子使用标准template-parts/content.php,也可以通过创建archive-team.phpcontent-team.php重写其中一个。并且the_posts_pagination()功能将在适当的存档页面上起作用(archive.phparchive-team.php)。


0
投票

我会尝试两件事。

  1. 将参数paged添加到您的wp_query args:

                $args = array(
                    'post_type' => 'team',
                    'post_status' => 'publish',
                    'paged' => $paged,
                    'posts_per_page' => 1,
                );
    
  2. 将分页模板放在循环之外:

                <?php endwhile; ?>
                <?php get_template_part('pagination'); ?>
    
© www.soinside.com 2019 - 2024. All rights reserved.