【问题标题】:How to add pagination to a custom Word Press loop?如何将分页添加到自定义 Word Press 循环?
【发布时间】:2020-11-10 15:19:31
【问题描述】:

这是我的代码 -

    <div class="gallery-posts">
        <?php
            $args = array( 
                'post_type' => 'post', 
                'order' => 'ASC',
                'numberposts' => 4
                );
            $product_posts = get_posts( $args ); 
        ?>
        <?php foreach ( $product_posts as $post ) : setup_postdata( $post ); ?>    
            <?php $videos = rwmb_meta( '_video-link' );
                    foreach ( $videos as $video ); ?>   
                <div 
                class="card-container video-link" 
                data-link="<?php echo $video['src']; ?>" 
                title="<?php the_title(); ?>"
                description="<?php echo ( get_post_meta( get_the_ID(), '_description', true ) ); ?>"
                thumbnail-link="<?php the_post_thumbnail_url()  ?>">
                    <figure class="gallery-image">
                        <?php the_post_thumbnail(); ?>
                        <div class="gallery-image-text">
                            <p><?php the_title() ?></p>
                            <i class="fas fa-play"></i>
                        </div>
                    </figure>
                    </div>
        <?php endforeach; wp_reset_postdata(); ?>
    </div>

我想设置它,以便在帖子超过 4 个时显示 Next/Prev。

使用常规的 Word Press 循环,我知道您可以只执行 the_posts_pagination,但这在这里不起作用。

需要添加什么才能使分页与此循环一起使用?

【问题讨论】:

    标签: php wordpress loops pagination


    【解决方案1】:

    像这样更改您的代码:

    <div class="gallery-posts">
        <?php
        $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
            $args = array( 
                'post_type' => 'post', 
                'order' => 'ASC',
                'posts_per_page' => 4,
                'paged' => $paged
                );
            $product_posts = get_posts( $args ); 
            foreach ( $product_posts as $post ) : setup_postdata( $post ); 
                $videos = rwmb_meta( '_video-link' );
                foreach ( $videos as $video ):
                ?>
                <div class="card-container video-link" data-link="<?php echo $video['src']; ?>" title="<?php the_title(); ?>"  description="<?php echo ( get_post_meta( get_the_ID(), '_description', true ) ); ?>" thumbnail-link="<?php the_post_thumbnail_url()  ?>">
                    <figure class="gallery-image">
                        <?php the_post_thumbnail(); ?>
                        <div class="gallery-image-text">
                            <p><?php the_title() ?></p>
                            <i class="fas fa-play"></i>
                        </div>
                    </figure>
                    </div>
                <?php
                
                next_posts_link( 'Older Entries', $product_posts->max_num_pages );
                previous_posts_link( 'Next Entries &raquo;' );
                wp_reset_postdata();
                endforeach;
        ?>
    </div>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-05-21
      • 1970-01-01
      • 1970-01-01
      • 2016-04-18
      • 2017-05-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多