前3个帖子的样式不同,并使用第二个循环来打断其余的帖子/偏移量和分页

时间:2017-01-23 作者:Holly

我正在处理一个主题,在使用offest时,我遇到了循环/分页中断的问题。我在网上查找,试图只使用1个循环(per this post) 但我很难做到这一点。

我希望页面顶部的前3篇文章是最新的(第1-3篇),然后底部的3篇文章是接下来的3篇(第4-6篇),当您单击分页时,我希望它位于顶部(第1-3篇)和第7-9篇。

现在代码在第一页正确显示的地方工作,但当我在分页上单击“后退”时,它只会在前一页反复显示完全相同的6篇文章。

请参见下面我的索引页的我的代码:

<?php get_header(); ?>

<div class="row post-carousel">
<?php
    $args = array(
    \'posts_per_page\' => \'3\',
    );

    $query = new WP_query ( $args );
    if ( $query->have_posts() ) { ?>

<?php while ( $query->have_posts() ) : $query->the_post(); /* start the loop */ ?>

<div class="col-xs-12 col-sm-4">
<article id="post-<?php the_ID(); ?>" <?php post_class( \'most-recent\' ); ?>>
    <?php if ( has_post_thumbnail() ) { ?>
        <a href="<?php the_permalink(); ?>">
                <div class="post-thumbnail-img"><?php the_post_thumbnail(\'index-carousel\'); ?></div>

        </a>
    <?php } ?>
    <?php the_title( sprintf( \'<h2 class="entry-title"><a href="%s" rel="bookmark">\', esc_url( get_permalink() ) ), \'</a></h2>\' ); ?>


</article><!-- #post-## -->
</div>

<?php // End the loop.
endwhile;

rewind_posts();

} ?>
</div>

<div class="row newsletter-container">
<div class="newsletter col-sm-12 col-md-6">
    <p>Sign up for my newsletter, for all the latest updates!</p>
</div>
<div class="newsletter col-sm-12 col-md-6">

<!-- Begin MailChimp Signup Form -->
<!-- code goes here -->
<!--End mc_embed_signup-->

</div>
</div>


    <?php query_posts(\'posts_per_page=3&offset=3\');
        if ( have_posts() ) : ?>

        <?php /* Start the Loop */ ?>
        <?php while ( have_posts() ) : the_post(); ?>

            <?php
                /* Include the Post-Format-specific template for the content.
                 * If you want to overload this in a child theme then include a file
                 * called content-___.php (where ___ is the Post Format name) and that will be used instead.
                 */
                get_template_part( \'content\', get_post_format() );
            ?>

        <?php endwhile; ?>

        <?php _tk_content_nav( \'nav-below\' ); ?>

    <?php else : ?>

        <?php get_template_part( \'no-results\', \'index\' ); ?>

    <?php endif; ?>

<?php get_footer(); ?>
此外,当我尝试将PHP替代语法用于控制结构时,它似乎破坏了代码,整个页面都变白了。

同时添加导航代码:

if ( ! function_exists( \'_tk_content_nav\' ) ) :
/**
 * Display navigation to next/previous pages when applicable
 */
function _tk_content_nav( $nav_id ) {
    global $wp_query, $post;

    // Don\'t print empty markup on single pages if there\'s nowhere to navigate.
    if ( is_single() ) {
        $previous = ( is_attachment() ) ? get_post( $post->post_parent ) : get_adjacent_post( false, \'\', true );
        $next = get_adjacent_post( false, \'\', false );

        if ( ! $next && ! $previous )
            return;
    }

    // Don\'t print empty markup in archives if there\'s only one page.
    if ( $wp_query->max_num_pages < 2 && ( is_home() || is_archive() || is_search() ) )
        return;

    $nav_class = ( is_single() ) ? \'post-navigation\' : \'paging-navigation\';

    ?>
    <nav role="navigation" id="<?php echo esc_attr( $nav_id ); ?>" class="<?php echo $nav_class; ?>">
        <h1 class="screen-reader-text"><?php _e( \'Post navigation\', \'_tk\' ); ?></h1>
        <ul class="pager">

        <?php if ( is_single() ) : // navigation links for single posts ?>

            <?php previous_post_link( \'<li class="nav-previous previous">%link</li>\', \'<span class="meta-nav">\' . _x( \'&larr;\', \'Previous post link\', \'_tk\' ) . \'</span> %title\' ); ?>
            <?php next_post_link( \'<li class="nav-next next">%link</li>\', \'%title <span class="meta-nav">\' . _x( \'&rarr;\', \'Next post link\', \'_tk\' ) . \'</span>\' ); ?>

        <?php elseif ( $wp_query->max_num_pages > 1 && ( is_home() || is_archive() || is_search() ) ) : // navigation links for home, archive, and search pages ?>

            <?php if ( get_next_posts_link() ) : ?>
            <li class="nav-previous previous"><?php next_posts_link( __( \'<span class="meta-nav">&larr;</span> Older posts\', \'_tk\' ) ); ?></li>
            <?php endif; ?>

            <?php if ( get_previous_posts_link() ) : ?>
            <li class="nav-next next"><?php previous_posts_link( __( \'Newer posts <span class="meta-nav">&rarr;</span>\', \'_tk\' ) ); ?></li>
            <?php endif; ?>

        <?php endif; ?>

        </ul>
    </nav><!-- #<?php echo esc_html( $nav_id ); ?> -->
    <?php
}
endif; // _tk_content_nav

1 个回复
SO网友:Tunji

我的建议是永远不要使用query_posts. 而是使用自定义查询或pre_get_posts 钩住这两个实例,并始终确保调用wp_reset_postdata 在自定义查询之后。

BUT 因为导航功能引用global $wp_query, 你必须使用query_posts 通过paged 参数。

<?php get_header(); ?>

    <div class="row post-carousel">
        <?php
        $args = array(
            \'posts_per_page\' => \'3\',
        );

        $query = new WP_query ( $args );
        if ( $query->have_posts() ) { ?>

            <?php while ( $query->have_posts() ) : $query->the_post(); /* start the loop */ ?>

                <div class="col-xs-12 col-sm-4">
                    <article id="post-<?php the_ID(); ?>" <?php post_class( \'most-recent\' ); ?>>
                        <?php if ( has_post_thumbnail() ) { ?>
                            <a href="<?php the_permalink(); ?>">
                                <div class="post-thumbnail-img"><?php the_post_thumbnail(\'index-carousel\'); ?></div>

                            </a>
                        <?php } ?>
                        <?php the_title( sprintf( \'<h2 class="entry-title"><a href="%s" rel="bookmark">\', esc_url( get_permalink() ) ), \'</a></h2>\' ); ?>


                    </article><!-- #post-## -->
                </div>

            <?php // End the loop.
            endwhile;
            wp_reset_postdata();

        } ?>
    </div>

    <div class="row newsletter-container">
        <div class="newsletter col-sm-12 col-md-6">
            <p>Sign up for my newsletter, for all the latest updates!</p>
        </div>
        <div class="newsletter col-sm-12 col-md-6">

            <!-- Begin MailChimp Signup Form -->
            <!-- code goes here -->
            <!--End mc_embed_signup-->

        </div>
    </div>


<?php
$paged = ( get_query_var(\'page\') ) ? get_query_var(\'page\') : 1;
$second_args = array(
    \'posts_per_page\' => 3,
    \'offset\' => 3,
    \'paged\' => $paged
);
query_posts($second_args);
if ( have_posts() ) : ?>

    <?php /* Start the Loop */ ?>
    <?php while ( have_posts() ) : the_post(); ?>

        <?php
        /* Include the Post-Format-specific template for the content.
         * If you want to overload this in a child theme then include a file
         * called content-___.php (where ___ is the Post Format name) and that will be used instead.
         */
        get_template_part( \'content\', get_post_format() );
        ?>

    <?php endwhile; wp_reset_postdata(); ?>

    <?php _tk_content_nav( \'nav-below\' ); ?>

<?php else : ?>

    <?php get_template_part( \'no-results\', \'index\' ); ?>

<?php endif; ?>

<?php get_footer(); ?>

相关推荐

Custom pagination structure

当前我的分页工作方式如下http://example.com/city/usa/ #for page 1 http://example.com/city/usa/page/2 http://example.com/city/usa/page/3 ...and so on 我想将其更改为:http://example.com/city/usa/ #for page 1 http:/