• 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)
  • Try is_front_page(), and use WP_Query() instead of query_posts()!

    Thread Starter InHouse

    (@inhouse)

    @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' );

    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: https://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.