David Moran
David Moran

Reputation: 177

Only run wordpress query in one section

I currently have a setup where i have the following files being called in order.

in my header.php, i am calling <?php get_theme_part( 'front', 'current-front' ); ?>

The contents of that file are

<?php query_posts("showposts=1&cat=6"); ?>

<?php
/**
 * The template for displaying list of recent posts on the front page.
 *
 * @package WordPress
 * @subpackage xxx
 * @since xxx 1.0
 */
if ( !have_posts() ) 
    return;
?>

<div class="front-current-print">
        <div class="current-print">
            <?php while ( have_posts() ) : the_post(); ?>
                <?php get_theme_part( 'current-print' ); ?>
            <?php endwhile; ?>
        </div>
</div>

That file is calling current-print.php, which has this code:

<?php
?><article <?php post_class( 'compact bg-secondary' ); ?>>
    <a href="<?php the_permalink() ?>"><?php the_post_thumbnail( 'current-print' ); ?></a>
    <h5 class="title">
        <a class="inverse" href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
    </h5>
    <div class="entry-summary"><?php the_excerpt(); ?></div>
    <?php the_post_episode(); ?>
</article>

This code allows me to have a thumbnail for a specific post from a specific category.

This works fine on the front page, but it seems that for all inside pages and posts it will only display the category and the post. I've tried taking out <?php query_posts("showposts=1&cat=6"); ?> and the posts come through fine. Is there any way i can have that script only run in the header and not affect the rest of the site?

Upvotes: 0

Views: 88

Answers (1)

vicente
vicente

Reputation: 2643

You need to use wp_reset_query();:

wp_reset_query() restores the $wp_query and global post data to the original main query. This function should be called after query_posts(), if you must use that function.

See: http://codex.wordpress.org/Function_Reference/wp_reset_query

In your case this would be:

<?php query_posts("showposts=1&cat=6"); ?>

<?php
/**
 * The template for displaying list of recent posts on the front page.
 *
 * @package WordPress
 * @subpackage xxx
 * @since xxx 1.0
 */
if ( !have_posts() ) 
    return;
?>

<div class="front-current-print">
        <div class="current-print">
            <?php while ( have_posts() ) : the_post(); ?>
                <?php get_theme_part( 'current-print' ); ?>
            <?php endwhile; ?>
        </div>
</div>

<?php wp_reset_query(); ?>

Upvotes: 1

Related Questions