简体   繁体   中英

Wordpress: creating 2 loops. First for one sticky OR the latest post. Second for all other posts

I'm new to Wordpress and PHP and I've been battling with this for a while now. What I have for the moment is 2 loops.

I want to first loop to only ever show 1 post: either a sticky post, or the latest post if there are no sticky posts.

The second loop should show all other recent posts, like a basic blog, except any posts which are stickied or the recent post which appears in the first loop.

What I have:

My code shows the latest post in the first loop and other posts in the second loop.

The problem:

When I make a sticky post, it appears in the first loop along with the latest post (I only want the sticky post to appear here in this situation and not both), and it also appears at the top of the second loop.

Here it is in action: http://bit.ly/1qS0MMC

I've managed to get this far but I can't seem to finish it.

<div id="main" class="site-main">
    <div id="primary" class="content-area">
        <div id="content" class="site-content" role="main">
            <div id="featuredsection">
               <?php $args = array(
                  'posts_per_page' => 1,
                     );
                   $my_query = new WP_Query( $args );
                   while ( $my_query->have_posts() ) : $my_query->the_post();
               $do_not_duplicate = $post->ID; ?>

                <div id="post-<?php the_ID(); ?>" <?php post_class( '' ); ?> >
                /*---Featured post template ---*/
                </div>
               <?php endwhile; ?>
            </div>
<div id="gridcontainer">
<?php
    $counter = 1; //start counter
    $grids = 2; //Grids per row
    global $query_string; //Need this to make pagination work

if ( have_posts() ) : while ( have_posts() ) : the_post();
if ( $post->ID == $do_not_duplicate ) continue; ?>

<?php
//Show the left hand side column
    if($counter == 1) :
?>
        <div id="post-<?php the_ID(); ?>" <?php post_class( 'griditemleft' ); ?> >
            /*---Left column post template ---*/
        </div>
<?php
//Show the right hand side column
elseif($counter == $grids) :
?>
        <div id="post-<?php the_ID(); ?>" <?php post_class( 'griditemright' ); ?>>
            /*---Right columnpost template ---*/
        </div>

<div class="clear"></div>
<?php
$counter = 0;
endif;
?>
<?php
$counter++;
endwhile;
//Pagination can go here if you want it.
endif;
?>
<?php
untitled_content_nav( 'nav-below' );
?>
    </div>
</div>

Would it be easier to create separate instances of WP_Query and loop for each post block required?

You can then pass your $arg parameters such as 'ignore_sticky_posts' => 1 too your required WP_Query loop.

http://codex.wordpress.org/Class_Reference/WP_Query

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM