分页在自定义帖子类型中不起作用

时间:2020-05-08 作者:I dont believe you

分页模板部分包括具有样式的通用分页函数。模板部分用于存档。php(它代表“single”,您知道默认的wp文件),但不适用于自定义post类型。

为什么?如何解决?

<?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 个回复
SO网友:Jacob Peattie

分类法存档模板不应要求自定义WP_Query. WordPress会自动查询正确的帖子。只需使用主回路,不使用主回路$loop->. 您已经这样做了,但出于某种原因对其进行了注释。

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