Title: Conditional Statement to Query Posts in Header
Last modified: August 20, 2016

---

# Conditional Statement to Query Posts in Header

 *  [InHouse](https://wordpress.org/support/users/inhouse/)
 * (@inhouse)
 * [13 years, 6 months ago](https://wordpress.org/support/topic/conditional-statement-to-query-posts-in-header/)
 * Hi, I’m trying to use query_posts in my header file and make it conditional so
   it only displays on the home page.
 * Currently, this displays on all pages and pulls in the content of the page rather
   than the post with ID 68. When I remove the conditional statement, it grabs post
   ID 68 perfectly, except it’s not conditional to the home page only.
 * I’m unsure of how to use the “if is_home” conditional statement with query_posts.
   I’m trying to put this together based on what I learned in Codex but this is 
   a bit more advanced than I can manage I think.
 * Thanks in advance for any help!
 *     ```
       if ( is_home() ) {
       // The Query
       query_posts( 'p=68' );
       }
       // The Loop
       while ( have_posts() ) : the_post();
       	echo '<div class="the_content" id="home-quote-container">';
       		echo '<div class="home-quote-img">';
       			the_post_thumbnail('home-quote-img');
       		echo '</div>';
       		echo '<div class="home-quote-text">';
       			echo '<div class="home-quote-content">';
       				the_content();
       			echo '</div>';
       			echo '<div class="home-quote-excerpt">';
       				the_excerpt();
       			echo '</div>';
       		echo '</div>';
       		echo '<div style="clear:both;"></div>';
       	echo '</div>';
       endwhile;
   
       // Reset Query
       wp_reset_query();
       ```
   

Viewing 3 replies - 1 through 3 (of 3 total)

 *  [Andrew Bartel](https://wordpress.org/support/users/andrew-bartel/)
 * (@andrew-bartel)
 * [13 years, 6 months ago](https://wordpress.org/support/topic/conditional-statement-to-query-posts-in-header/#post-3171909)
 * Try is_front_page(), and use WP_Query() instead of query_posts()!
 *  Thread Starter [InHouse](https://wordpress.org/support/users/inhouse/)
 * (@inhouse)
 * [13 years, 6 months ago](https://wordpress.org/support/topic/conditional-statement-to-query-posts-in-header/#post-3171913)
 * [@andrew](https://wordpress.org/support/users/andrew/), sorry for my ignorance,
   but what do I need to change to use WP_Query()? I must be using it incorrectly
   because it’s still not conditional. It appears on every page and mirrors the_content
   for whatever page you’re viewing still.
 *     ```
       if (is_front_page()) {
       // The Query
       WP_Query( 'p=68' );
       }
   
       // The Loop
       while ( have_posts() ) : the_post();
       	echo '<div class="the_content" id="home-quote-container">';
       		echo '<div class="home-quote-img">';
       			the_post_thumbnail('home-quote-img');
       		echo '</div>';// end .home-quote-img
       		echo '<div class="home-quote-text">';
       			echo '<div class="home-quote-content">';
       				the_content();
       			echo '</div>';// end .home-quote-content
       			echo '<div class="home-quote-excerpt">';
       				the_excerpt();
       			echo '</div>';// end .home-quote-excerpt
       		echo '</div>';// end .home-quote-text
       		echo '<div style="clear:both;"></div>';
       	echo '</div>';// end #home-quote-container
       endwhile;
   
       // Reset Query
       wp_reset_query();
       ```
   
 * Or
 *     ```
       $query = new WP_Query( 'p=68' );
       ```
   
 *  [Andrew Bartel](https://wordpress.org/support/users/andrew-bartel/)
 * (@andrew-bartel)
 * [13 years, 6 months ago](https://wordpress.org/support/topic/conditional-statement-to-query-posts-in-header/#post-3171922)
 * So, only the query_posts and WP_Query part are in your conditional statement.
   You’ll want the whole piece of code in there. Below:
 *     ```
       if (is_front_page()) {
       // The Query
       $homepage_query = new WP_Query( 'p=68' );
   
       // The Loop
       while ( $homepage_query->have_posts() ) : $homepage_query->the_post();
       	echo '<div class="the_content" id="home-quote-container">';
       		echo '<div class="home-quote-img">';
       			the_post_thumbnail('home-quote-img');
       		echo '</div>';// end .home-quote-img
       		echo '<div class="home-quote-text">';
       			echo '<div class="home-quote-content">';
       				the_content();
       			echo '</div>';// end .home-quote-content
       			echo '<div class="home-quote-excerpt">';
       				the_excerpt();
       			echo '</div>';// end .home-quote-excerpt
       		echo '</div>';// end .home-quote-text
       		echo '<div style="clear:both;"></div>';
       	echo '</div>';// end #home-quote-container
       endwhile;
   
       // Reset Query
       wp_reset_query();
       }
       ```
   
 * Here is a nice breakdown of why not to use query_posts: [http://wordpress.stackexchange.com/questions/1753/when-should-you-use-wp-query-vs-query-posts-vs-get-posts](http://wordpress.stackexchange.com/questions/1753/when-should-you-use-wp-query-vs-query-posts-vs-get-posts)
 * Happy coding! Let me know if that doesn’t work for you.

Viewing 3 replies - 1 through 3 (of 3 total)

The topic ‘Conditional Statement to Query Posts in Header’ is closed to new replies.

## Tags

 * [conditional](https://wordpress.org/support/topic-tag/conditional/)
 * [posts](https://wordpress.org/support/topic-tag/posts/)
 * [query](https://wordpress.org/support/topic-tag/query/)
 * [query_posts](https://wordpress.org/support/topic-tag/query_posts/)

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 3 replies
 * 2 participants
 * Last reply from: [Andrew Bartel](https://wordpress.org/support/users/andrew-bartel/)
 * Last activity: [13 years, 6 months ago](https://wordpress.org/support/topic/conditional-statement-to-query-posts-in-header/#post-3171922)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
