我想在博客的第一篇文章中使用不同的HTML。
我做了两个循环。不幸的是,这已停止分页。我想这是因为我正在创建一个新的查询来解决这个问题。
如何在仍有工作分页的情况下解决问题?
原始单循环代码
<?php # The Loop
if ( have_posts()): ?>
<div class="row">
<?php # Loop through
while ( have_posts() ) : the_post();
include( \'includes/snippets/\'.$post->post_type.\'-card.php\');
endwhile; ?>
</div>
<?php else:
echo \'<div class="alert alert-warning">No results found.</div>\';
endif; ?>
我的双环尝试
<?php
$args = array(
\'posts_per_page\' => \'1\',
);
$query = new WP_query ( $args );
if ( $query->have_posts() ) { ?>
<?php while ( $query->have_posts() ) : $query->the_post(); /* start the loop */ ?>
<?php include( \'includes/snippets/\'.$post->post_type.\'-card-large.php\'); ?>
<?php // End the loop.
endwhile;
}
$args = array(
\'posts_per_page\' => \'11\',
\'offset\' => \'1\',
);
$query = new WP_query ( $args );
if ( $query->have_posts() ) { ?>
<?php while ( $query->have_posts() ) : $query->the_post(); /* start the loop */ ?>
<?php include( \'includes/snippets/\'.$post->post_type.\'-card.php\'); ?>
<?php // End the loop.
endwhile;
} ?>
<div class="nav-previous"><?php next_posts_link( __( \'<span class="meta-nav">←</span> Older posts\', \'twentyten\' ) ); ?></div>
<div class="nav-next"><?php previous_posts_link( __( \'Newer posts <span class="meta-nav">→</span>\', \'twentyten\' ) ); ?></div>
最合适的回答,由SO网友:ChristopherJones 整理而成
这是我的尝试。。。虽然我真的没有一个好的地方来测试它,但我认为它应该起作用。(祈祷好运)
<?php
// First Page of Pagination - https://codex.wordpress.org/Function_Reference/is_paged
if(!is_paged()){
$first_page = true;
$query_args = array(
\'posts_per_page\' => \'11\',
);
} else {
$query_args = array(
\'posts_per_page\' => \'10\',
\'offset\' => \'1\',
);
}
$query = new WP_query ( $query_args );
if ( $query->have_posts() ) {
while ( $query->have_posts() ) : $query->the_post();
// Let\'s deal with the first post on the first page
if($first_page===true && $wp_query->current_post===0){
include( \'includes/snippets/\'.$post->post_type.\'-card-large.php\');
} else {
include( \'includes/snippets/\'.$post->post_type.\'-card.php\');
}
endwhile;
}
?>
<div class="nav-previous"><?php next_posts_link( __( \'<span class="meta-nav">←</span> Older posts\', \'twentyten\' ) ); ?></div>
<div class="nav-next"><?php previous_posts_link( __( \'Newer posts <span class="meta-nav">→</span>\', \'twentyten\' ) ); ?></div>
试过之后让我知道你那边的情况,看看我们是否能解决!