你能帮我找到适合这个循环的分页代码吗? 这是我的循环:
<?php query_posts('showposts=1&post_type=post'); ?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
...
<?php endwhile; else: ?>
...
<?php endif; ?>
<?php rewind_posts(); ?>
<?php query_posts('showposts=2&offset=1&post_type=post'); ?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
...
<?php endwhile; else: ?>
...
<?php endif; ?>
<?php rewind_posts(); ?>
<?php query_posts('showposts=6&offset=3&post_type=post'); ?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
...
<?php endwhile; else: ?>
...
<?php endif; ?>
修改
这是我的旧循环,如何将其转换为新循环?不幸的是,我不知道php是如何运作的。
<?php
if (is_front_page() ) {
get_header( 'front' );
} else {
get_header();
}
?>
<section id="content" class="container">
<div class="row">
<div class="col-md-7 col-sm-7">
<?php query_posts('showposts=1&post_type=post'); ?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<article class="thumbnail thumbnail-principale expand-image">
<div class="featured-image">
<a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"><?php echo get_the_post_thumbnail($post_id, 'large', array('class' => 'img-responsive')); ?></a>
</div>
<div class="destacado"><h3>Destacado</h3></div>
<header class="testo-articolo">
<h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
<div class="entry-meta">
<p class="text-muted resume"><span><i class="fa fa-calendar"></i><?php the_time('j M y'); ?></span><span><i class="glyphicon glyphicon-user"></i><?php the_author(); ?></span><span><i class="fa fa-comment-o"></i><?php comments_popup_link('0', '1 Comentario', '% Comentarios'); ?></span></p>
</div>
<p><?php the_excerpt(); ?></p>
<?php comments_template(); ?><!-- da sistemare -->
</header>
</article>
</div>
<?php endwhile; else: ?>
<p>Sorry, there are no posts to display</p>
<?php endif; ?>
<?php rewind_posts(); ?>
<?php query_posts('showposts=2&offset=1&post_type=post'); ?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<div class="col-md-5 col-sm-5">
<article class="thumbnail thumbnail-destra expand-image">
<div class="featured-image">
<a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"><?php echo get_the_post_thumbnail($post_id, 'large', array('class' => 'img-responsive')); ?></a>
</div>
<header class="testo-articolo-destra expand-image">
<h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
<div class="entry-meta">
<p class="text-muted resume"><span><i class="fa fa-calendar"></i><?php the_time('j M y'); ?></span><span><i class="glyphicon glyphicon-user"></i><?php the_author(); ?></span><span><i class="fa fa-comment-o"></i><?php comments_popup_link('0', '1 Comentario', '% Comentarios'); ?></span></p>
</div>
<p><?php the_excerpt(); ?></p>
</header>
<div class="badge1"></div>
</article>
</div>
<?php endwhile; else: ?>
<p>Sorry, there are no posts to display</p>
<?php endif; ?>
</div><!-- /.row -->
<div class="row">
<?php rewind_posts(); ?>
<?php query_posts('showposts=6&offset=3&post_type=post'); ?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<div class="col-md-4 col-sm-4">
<article class="thumbnail distanza expand-image">
<div class="featured-image">
<a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"><?php echo get_the_post_thumbnail($post_id, 'large', array('class' => 'img-responsive ingrandire-img')); ?></a>
</div>
<header class="testo-articolo">
<h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
<div class="entry-meta">
<p class="text-muted resume"><span><i class="fa fa-calendar"></i><?php the_time('j M y'); ?></span><span><i class="glyphicon glyphicon-user"></i><?php the_author(); ?></span><span><i class="fa fa-comment-o"></i><?php comments_popup_link('Ningún comentario', '1 Comentario', '% Comentarios'); ?></span></p>
</div>
<p><?php the_excerpt(); ?></p>
<p><a class="btn btn-default read-more" href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"><?php _e( 'Lee más', 'katartika' ); ?></a></p>
</header>
</article>
</div>
<?php endwhile; else: ?>
<p>Sorry, there are no posts to display</p>
<?php endif; ?>
</div><!-- /row -->
<div style="text-align:center;">
<?php posts_nav_link(‘|’, ‘Prossimo’, ‘Precedente’); ?>
</div>
</section>
</div><!-- /sezione -->
<?php get_footer(); ?>
答案 0 :(得分:0)
正如我昨晚所述,您可以在一个查询中执行此操作,而无需自定义查询。
我想强制您永远使用query_posts
这一事实。 query_posts
打破分页,重新运行查询,降低页面性能,并打破许多插件或自定义函数所需的主查询对象。这是一种运行自定义查询的邪恶方式,应该不惜一切代价避免这种查询。如果您需要运行自定义查询,请对分页查询使用WP_Query
,对非分页查询使用get_posts
。
您也不应该在主页和任何存档类型页面上使用自定义查询替换主查询,这会破坏页面功能。自定义查询应仅针对辅助帖子(如幻灯片,精选内容,相关和热门帖子以及页面模板和静态首页)运行,以显示自定义帖子。如果您需要更改主页或存档页面上的主查询,请使用pre_get_posts
现在,要解决您的问题,您需要来自主查询和计数器的一个循环,在这种情况下,我们将使用主查询计数器$wp_query->current_post
中的构建(一个注释,此计数器从{{1开始)因此,在循环中发布一个将是0
,后两个将是0
,等等。
根据我从您的代码中读到的内容,第一个代码需要与第二个和第三个代码不同,这些代码与后六个代码不同
让我们把它放入代码
1
您可以将代码转换为
if ( have_posts() ) {
while ( have_posts() ) {
the_post();
/*
* Display different outlay for post one
*/
if ( $wp_query->current_post === 0 ) {
// Do what needs to be done for post one
} elseif ( $wp_query->current_post >= 1 && $wp_query->current post <= 2 ) {
/*
* Do something for posts 2 and 3
*/
} elseif ( $wp_query->current_post >= 3 ) {
/*
* Do something for the rest of the posts
*/
}
}
}
答案 1 :(得分:0)
我不知道这是否正确,但我明白了:
<?php
if (is_front_page() ) {
get_header( 'front' );
} else {
get_header();
}
?>
<section id="content" class="container">
<div class="row">
<?php if ( have_posts() ) : while ( have_posts() ) : the_post();
if (!is_paged() && $wp_query->current_post === 0 ) { ?>
<div class="col-md-7 col-sm-7">
<article class="thumbnail thumbnail-principale expand-image">
<div class="featured-image">
<a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"><?php echo get_the_post_thumbnail($post_id, 'large', array('class' => 'img-responsive')); ?></a>
</div>
<div class="destacado"><h3>Destacado</h3></div>
<header class="testo-articolo">
<h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
<div class="entry-meta">
<p class="text-muted resume"><span><i class="fa fa-calendar"></i><?php the_time('j M y'); ?></span><span><i class="glyphicon glyphicon-user"></i><?php the_author(); ?></span><span><i class="fa fa-comment-o"></i><?php comments_popup_link('0', '1 Comentario', '% Comentarios'); ?></span></p>
</div>
<p><?php the_excerpt(); ?></p>
<?php comments_template(); ?><!-- da sistemare -->
</header>
</article>
</div>
<?php
} elseif (!is_paged() && $wp_query->current_post >= 1 && $wp_query->current_post <= 2 ) { ?>
<div class="col-md-5 col-sm-5">
<article class="thumbnail thumbnail-destra expand-image">
<div class="featured-image">
<a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"><?php echo get_the_post_thumbnail($post_id, 'large', array('class' => 'img-responsive')); ?></a>
</div>
<header class="testo-articolo-destra expand-image">
<h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
<div class="entry-meta">
<p class="text-muted resume"><span><i class="fa fa-calendar"></i><?php the_time('j M y'); ?></span><span><i class="glyphicon glyphicon-user"></i><?php the_author(); ?></span><span><i class="fa fa-comment-o"></i><?php comments_popup_link('0', '1 Comentario', '% Comentarios'); ?></span></p>
</div>
<p><?php the_excerpt(); ?></p>
</header>
<div class="badge1"></div>
</article>
</div>
<?php
} elseif ( $wp_query->current_post >= 3 ) { ?>
<div class="col-md-4 col-sm-4">
<article class="thumbnail distanza expand-image">
<div class="featured-image">
<a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"><?php echo get_the_post_thumbnail($post_id, 'large', array('class' => 'img-responsive ingrandire-img')); ?></a>
</div>
<header class="testo-articolo">
<h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
<div class="entry-meta">
<p class="text-muted resume"><span><i class="fa fa-calendar"></i><?php the_time('j M y'); ?></span><span><i class="glyphicon glyphicon-user"></i><?php the_author(); ?></span><span><i class="fa fa-comment-o"></i><?php comments_popup_link('Ningún comentario', '1 Comentario', '% Comentarios'); ?></span></p>
</div>
<p><?php the_excerpt(); ?></p>
<p><a class="btn btn-default read-more" href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"><?php _e( 'Lee más', 'katartika' ); ?></a></p>
</header>
</article>
</div>
<?php
//new request for all the rest of post from page 2 onwards
} elseif ( is_paged() ) { ?>
<div class="col-md-4 col-sm-4">
<article class="thumbnail distanza expand-image">
<div class="featured-image">
<a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"><?php echo get_the_post_thumbnail($post_id, 'large', array('class' => 'img-responsive ingrandire-img')); ?></a>
</div>
<header class="testo-articolo">
<h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
<div class="entry-meta">
<p class="text-muted resume"><span><i class="fa fa-calendar"></i><?php the_time('j M y'); ?></span><span><i class="glyphicon glyphicon-user"></i><?php the_author(); ?></span><span><i class="fa fa-comment-o"></i><?php comments_popup_link('Ningún comentario', '1 Comentario', '% Comentarios'); ?></span></p>
</div>
<p><?php the_excerpt(); ?></p>
<p><a class="btn btn-default read-more" href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"><?php _e( 'Lee más', 'katartika' ); ?></a></p>
</header>
</article>
</div>
<?php }
endwhile;
endif; ?>
</div><!-- /row -->
<div style="text-align:center;">
<?php posts_nav_link('|', 'Prossimo', 'Precedente'); ?>
</div>
</section>
</div><!-- /sezione -->
<?php get_footer(); ?>