Skip to content

Instantly share code, notes, and snippets.

@davidvandenbor
Created July 31, 2014 21:08
Show Gist options
  • Select an option

  • Save davidvandenbor/3547fdabaef6b07bc1e5 to your computer and use it in GitHub Desktop.

Select an option

Save davidvandenbor/3547fdabaef6b07bc1e5 to your computer and use it in GitHub Desktop.
Multiple query_posts with rewinds
<?php
// WARNING: query_posts() will change your main query and is not recommended
// for secondary loops! Only use if absolutely necessary!!!!
// Creating a new instance of WP_Query or get_posts() is preferred for secondary loops.
// Check lines 10 and 22:
// Line 1 is telling WordPress to only display 1 post,
// which will automatically be the most recent one.
// On Line 22 we are telling WordPress to offset the posts by 1,
// which means it will not repeat the post output in the first loop.
// We are also limiting it to only displaying 3 posts.
?>
<?php query_posts('showposts=1&post_type=post'); ?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<h1><?php the_title() ;?></h1>
<?php the_post_thumbnail(); ?>
<?php the_excerpt(); ?>
<?php endwhile; else: ?>
<p>Sorry, there are no posts to display</p>
<?php endif; ?>
<hr>
<?php rewind_posts(); //rewind is used to rewind the loop and keep database call open ?>
<?php query_posts('showposts=3&offset=1&post_type=post'); ?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<h2><?php the_title() ;?></h2>
<?php the_excerpt(); ?>
<?php endwhile; else: ?>
<p>Sorry, there are no posts to display</p>
<?php endif; ?>
<?php
/**
* -------------------------------------------------------------------------------//
* some of the standard conditional statements you can use are:
*
* is_home() – Returns true if the current page is the homepage
* is_admin() – Returns true if admin is logged in and visiting the site
* is_single( array( 'foo', 'bar', 'baz' ) ) – Returns true if page is displaying a single post
* is_page() – Returns true if page is displaying a single page
* is_page_template() – Can tell you if page is using a specific template. i.e. is_page_template(‘about-page.php’)
* is_category() – Returns true if page or post has a specified category. i.e. is_category(‘news’);
* is_tag() – Returns true if page or post has a specified tag
* is_post_type_archive( $post_type ) Returns true if on a post type archive page that matches $post_type.
* is_author() – Returns true if a specific author is logged in and visiting the site. i.e. is_author(‘zgordon’)
* is_search – Returns true if page is a search results page
* is_404() – Returns true if page does not exist
* has_excerpt() – Returns true if post or page has an excerpt
* -------------------------------------------------------------------------------//
*/ ?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment