Include template before a loop

This is my category / archive page

i want to include a template before loop

Read More

but i cant get posts based on category, it showing latest post only on every page.

when i removed this line before the loop

<?php include( TEMPLATEPATH . '/includes/tb/hot_posts.tpl.php' ); ?>

it work smoothly, but i need this template before the loop

this is code (archive / category )

<div class="Hot_post">

<?php include( TEMPLATEPATH . '/includes/tb/hot_posts.tpl.php' ); ?>

<?php if (have_posts()) : $count = 0; ?>

and this is code from template (includes/tb/hot_posts.tpl.php)

 <?php query_posts('meta_key=post_views_count&order=DESC'); ?> 
 <?php if ( have_posts() ) : while (   have_posts() ) : the_post(); ?> 

please guide me

Related posts

Leave a Reply

2 comments

  1. Your problem has nothing at all to do with including a template-part file (for which, by the way, you should be using get_template_part() or locate_template()), and everything to do with Doing It Wrong with respect to query_posts().

    If you’re trying to override the main Loop query, then you need to make sure you’re doing so correctly. If you need to maintain the default query context, and append query arguments, then you have to maintain the default $wp_query. The easiest way to do so is as described in the Codex:

    global $wp_query;
    $args = array_merge( $wp_query->query, array( 'post_type' => 'product' ) );
    query_posts( $args );
    

    To use your custom arguments, for example:

    // Globalize $wp_query
    global $wp_query;
    // Define custom query args
    $custom_query_args = array(
        'meta_key' => 'post_views_count',
        'order' => 'DESC'
    );
    // Merge custom query args with default query
    $custom_query = array_merge( $wp_query->query, $custom_query_args );
    // Query posts based on modified query arguments
    query_posts( $custom_query );
    

    If, by chance, you’re intending to perform a secondary loop, then you don’t want to use query_posts() at all, but instead need to use WP_Query() or get_posts().