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

时间:2020-05-08 08:15:47

标签: php pagination wordpress-theming custom-post-type

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

为什么不呢?如何解决?

<?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>

1 个答案:

答案 0 :(得分:0)

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

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

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

然后,您可以在整个团队归档页面中使用标准archive.php,而在循环中为一个帖子使用标准template-parts/content.php,也可以通过创建archive-team.php或{{1} }。并且content-team.php函数将在适当的存档页面(the_posts_pagination()archive.php)上运行。

相关问题