首页 > 解决方案 > 自定义循环上的 Wordpress 分页

问题描述

我在下面有一些代码,第二页之后分页似乎失败了。我确定我在其中一个循环上做错了什么。

有人可以为我检查下面的代码吗?

<?php get_header();?>
<!-- Carousel -->
<div class="container fullwidth">
    <div id="myCarousel" class="carousel slide" data-ride="carousel">

      <!-- Wrapper for slides -->
      <div class="carousel-inner">

        <div class="item active" style="background:url(<?php echo esc_url( home_url( '/' ) ); ?>wp-content/uploads/2018/09/testphoto.jpg);">
        <div class="container">
           <div class="carousel-caption microstory">
            <h1>Backstage Blog</h1>
          </div>
        </div>
        </div><!-- End Item -->

      </div><!-- End Carousel Inner -->

    </div>
</div>
<!-- End Carousel -->

<!-- Posts -->
<div class="container paddingtop paddingbottom">
<?php $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
// are we on page one?
if(1 == $paged) { ?>
            <div class="row row-eq-height paddingbottom">
                <?php $query = new WP_Query( 'post_type=post&order=DESC&orderby=date&posts_per_page=1' ); ?>
                <?php if ( $query->have_posts() ) :?>   
                <?php while ( $query->have_posts() ) : $query->the_post(); ?>
                <div class="col col-lg-6 col-md-6 col-sm-6 col-xs-12 postpaddingbottom">
                    <div class="col-lg-12 col-xs-12">
                        <div class="featuredimage"><a href="<?php the_permalink(); ?>"><?php the_post_thumbnail('full'); ?></a></div>
                         <div class="blog-column">
                         <div class="titledatemain">
                            <h4><?php the_title(); ?></h4>
                             <ul class="blog-detail"> 
                                <li><i class="fa fa-calendar"></i> <?php the_time('F jS, Y'); ?></li> 
                            </ul> 
                            <p><?php the_excerpt(); ?></p>
                         </div>
                            <a href="<?php the_permalink(); ?>" class="buttonblack">Read More</a>
                        </div>
                    </div>
                </div>
                <?php endwhile;?> 
                <?php endif; ?>
                <?php $query2 = new WP_Query( 'post_type=advert&order=DESC&orderby=date&posts_per_page=1' ); ?>
                <?php if ( $query2->have_posts() ) :?>  
                <?php while ( $query2->have_posts() ) : $query2->the_post(); 
            $advert_url = get_post_meta($post->ID, 'url', true); 
            $featured_img_url = get_the_post_thumbnail_url(get_the_ID(),'full'); ?>
                    <div class="col col-lg-6 col-md-6 col-sm-6 col-xs-12 postpaddingbottom">
                    <a href="<?php echo $advert_url ?>"><div class="col-lg-12 col-xs-12 advertbox" style="background:url(<?php echo $featured_img_url ?>);">
                    </div></a>
                    </div>
                <?php endwhile; endif; ?>
           </div>              


<?php  
$query = new WP_Query( 'post_type=post&order=DESC&orderby=date&posts_per_page=3&offset=1&paged='. $paged ); ?>
<?php if ( $query->have_posts() ) :?>              
           <div class="row row-eq-height paddingbottom">
           <?php while ( $query->have_posts() ) : $query->the_post(); ?>
                <div class="col col-lg-4 col-md-4 col-sm-4 col-xs-12 postpaddingbottom">
                    <div class="col-lg-12 col-xs-12">
                        <a href="<?php the_permalink(); ?>"><div class="featuredimage blogimagesmall" style="background:url(<?php the_post_thumbnail_url('full'); ?>);"></div></a>
                         <div class="blog-colum">
                            <div class="titledate">
                                <a href="<?php the_permalink(); ?>"><h4><?php the_title(); ?></h4></a>
                             <ul class="blog-detail"> 
                                <li><i class="fa fa-calendar"></i> <?php the_time( 'F jS, Y' ); ?></li> 
                            </ul> 
                            </div>
                        </div>
                    </div>
                </div>
                    <?php endwhile;?>
            </div>

<?php endif; ?>

<?php } else { ?>

<?php  
$query = new WP_Query( 'post_type=post&order=DESC&orderby=date&posts_per_page=12&offset=4&paged='. $paged ); ?>
<?php if ( $query->have_posts() ) :?>              
           <div class="row row-eq-height paddingbottom">
           <?php while ( $query->have_posts() ) : $query->the_post(); ?>
                <div class="col col-lg-4 col-md-4 col-sm-4 col-xs-12 postpaddingbottom">
                    <div class="col-lg-12 col-xs-12">
                        <a href="<?php the_permalink(); ?>"><div class="featuredimage blogimagesmall" style="background:url(<?php the_post_thumbnail_url('full'); ?>);"></div></a>
                         <div class="blog-colum">
                            <div class="titledate">
                                <a href="<?php the_permalink(); ?>"><h4><?php the_title(); ?></h4></a>
                             <ul class="blog-detail"> 
                                <li><i class="fa fa-calendar"></i> <?php the_time( 'F jS, Y' ); ?></li> 
                            </ul> 
                            </div>
                        </div>
                    </div>
                </div>
                <?php endwhile;?>
            </div>
<?php 
wp_reset_postdata();
endif; ?>

<?php } ?>
<div class="row paddingbottom">
<div class="pagenav">
    <div class="alignleft"><?php previous_posts_link('Newer Posts', $query-   >max_num_pages) ?></div>
    <div class="alignright"><?php next_posts_link('Older Posts', $query->max_num_pages) ?></div>
</div>
</div>
</div>
<!-- End Posts -->

<?php get_footer();?>

我确定我的循环中只是一个错误。从本质上讲,我在第一页上的布局与其余页面不同(如您所见)。

似乎在第 2 页之后,当您转到第 3 页时,它只是一直显示第 2 页内容。

任何帮助都会很棒。

标签: phpwordpress

解决方案


这是更新的代码:

            <?php /* Template Name: Blog Page */ ?>
            <?php get_header();?>
            <!-- Carousel -->
            <div class="container fullwidth">
                <div id="myCarousel" class="carousel slide" data-ride="carousel">

                  <!-- Wrapper for slides -->
                  <div class="carousel-inner">

                    <div class="item active" style="background:url(<?php echo esc_url( home_url( '/' ) ); ?>wp-content/uploads/2018/09/testphoto.jpg);">
                    <div class="container">
                       <div class="carousel-caption microstory">
                        <h1>Backstage Blog</h1>
                      </div>
                    </div>
                    </div><!-- End Item -->

                  </div><!-- End Carousel Inner -->

                </div>
            </div>
            <!-- End Carousel -->

            <!-- Posts -->
            <div class="container paddingtop paddingbottom">
            <?php $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
            // are we on page one?

            $query = new WP_Query( 'post_type=post&order=DESC&orderby=date&posts_per_page=12&paged='. $paged );
            if(1 == $paged) { ?>
            <?php if ( $query->have_posts() ) { ?>  
                        <div class="row row-eq-height paddingbottom">
                        <?php $temp_query2 = $wp_query2; ?>
                            <?php $query3 = new WP_Query( 'post_type=post&order=DESC&orderby=date&posts_per_page=1' ); ?>   
                            <?php while ( $query3->have_posts() ) : $query3->the_post(); ?>
                            <div class="col col-lg-6 col-md-6 col-sm-6 col-xs-12 postpaddingbottom">
                                <div class="col-lg-12 col-xs-12">
                                    <div class="featuredimage"><a href="<?php the_permalink(); ?>"><?php the_post_thumbnail('full'); ?></a></div>
                                     <div class="blog-column">
                                     <div class="titledatemain">
                                        <h4><?php the_title(); ?></h4>
                                         <ul class="blog-detail"> 
                                            <li><i class="fa fa-calendar"></i> <?php the_time('F jS, Y'); ?></li> 
                                        </ul> 
                                        <p><?php the_excerpt(); ?></p>
                                     </div>
                                        <a href="<?php the_permalink(); ?>" class="buttonblack">Read More</a>
                                    </div>
                                </div>
                            </div>
                            <?php endwhile; ?>
                            <?php $wp_query2 = $temp_query2; ?>

                            <?php $temp_query = $wp_query; ?>
                            <?php $query2 = new WP_Query( 'post_type=advert&order=DESC&orderby=date&posts_per_page=1' ); ?> 
                            <?php while ( $query2->have_posts() ) : $query2->the_post(); 
                            $advert_url = get_post_meta($post->ID, 'url', true); 
                            $featured_img_url = get_the_post_thumbnail_url(get_the_ID(),'full'); ?>
                                <div class="col col-lg-6 col-md-6 col-sm-6 col-xs-12 postpaddingbottom">
                                <a href="<?php echo $advert_url ?>"><div class="col-lg-12 col-xs-12 advertbox" style="background:url(<?php echo $featured_img_url ?>);">
                                </div></a>
                                </div>
                            <?php endwhile; ?>
                            <?php $wp_query = $temp_query; ?>
                       </div>
                       <div class="row row-eq-height paddingbottom">
                       <?php while ( $query->have_posts() ) : $query->the_post(); ?>
                            <div class="col col-lg-4 col-md-4 col-sm-4 col-xs-12 postpaddingbottom">
                                <div class="col-lg-12 col-xs-12">
                                    <a href="<?php the_permalink(); ?>"><div class="featuredimage blogimagesmall" style="background:url(<?php the_post_thumbnail_url('full'); ?>);"></div></a>
                                     <div class="blog-colum">
                                        <div class="titledate">
                                            <a href="<?php the_permalink(); ?>"><h4><?php the_title(); ?></h4></a>
                                         <ul class="blog-detail"> 
                                            <li><i class="fa fa-calendar"></i> <?php the_time( 'F jS, Y' ); ?></li> 
                                        </ul> 
                                        </div>
                                    </div>
                                </div>
                            </div>
                            <?php endwhile;
                            rewind_posts();
                            }?>
                        </div>
            <?php } else { ?>
                       <div class="row row-eq-height paddingbottom">
                       <?php while ( $query->have_posts() ) : $query->the_post(); ?>
                            <div class="col col-lg-4 col-md-4 col-sm-4 col-xs-12 postpaddingbottom">
                                <div class="col-lg-12 col-xs-12">
                                    <a href="<?php the_permalink(); ?>"><div class="featuredimage blogimagesmall" style="background:url(<?php the_post_thumbnail_url('full'); ?>);"></div></a>
                                     <div class="blog-colum">
                                        <div class="titledate">
                                            <a href="<?php the_permalink(); ?>"><h4><?php the_title(); ?></h4></a>
                                         <ul class="blog-detail"> 
                                            <li><i class="fa fa-calendar"></i> <?php the_time( 'F jS, Y' ); ?></li> 
                                        </ul> 
                                        </div>
                                    </div>
                                </div>
                            </div>
                            <?php endwhile;?>
                        </div>
            <?php wp_reset_postdata(); ?>
            <?php } ?>
            <div class="row paddingbottom">
            <div class="pagenav">
                <div class="alignleft"><?php previous_posts_link('Newer Posts', $query->max_num_pages) ?></div>
                <div class="alignright"><?php next_posts_link('Older Posts', $query->max_num_pages) ?></div>
            </div>
            </div>
            </div>
            <!-- End Posts -->

            <?php get_footer();?>

你应该看到我现在正在运行一个很好的循环。但是我已经将第一篇文章放在一个循环中它自己的循环中以显示第一篇文章。我现在遇到的问题是我想在主循环上,并且只在第一页上,偏移量为 1。但我不知道如何在第一页上做到这一点:(


推荐阅读