Skip to content

Instantly share code, notes, and snippets.

@hans2103
Last active November 16, 2015 16:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hans2103/54b1c3e04beff3a653cd to your computer and use it in GitHub Desktop.
Save hans2103/54b1c3e04beff3a653cd to your computer and use it in GitHub Desktop.
WordPress - display first (sticky or not) post and then the rest of all posts (sticky or not).
if ( is_home() && !is_paged() ) :
$args = array (
// display the first sticky post, if none return the last post published
'posts_per_page' => 1,
'post__in' => get_option( 'sticky_posts' ),
'ignore_sticky_posts' => 1,
);
$query = new WP_Query( $args );
while ($query->have_posts()) : $query->the_post();
$do_not_duplicate = $post->ID;
echo '<br />';
echo the_title();
echo '<br />';
endwhile;
wp_reset_query();
echo '<br />';
echo '<br />';
$args = array (
// display the rest of the posts, except for the post shown above (sticky or first)
'posts_per_page' => 9,
'ignore_sticky_posts' => 1,
'post__not_in' => array($do_not_duplicate)
);
$query = new WP_Query( $args );
if( $query->have_posts() ) :
while ($query->have_posts()) : $query->the_post();
if( $post->ID == $do_not_duplicate ) continue;
echo the_title();
echo '<br />';
endwhile;
endif;
endif;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment