Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have a Featured Post Category. Im marked some post Featured. When i viewing a category page(e.g. Sport), i want to list on sidebar both marked post list (e.g Featured and Sport,)

I tryed with that following code.It's worked for me but when i using my code i see two category post (e.g Sport ), category post list affected by my code and listing same post list.

My example picture! Here

How can i fix that? My Code:

<?php
//55 is my Featured Category id
if ( is_category() ) {
  $current_cat = get_query_var('cat');
}
query_posts(array('category__and' => array(55, $current_cat )));

if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>

        <li><a href="<?php the_permalink(); ?>">
          <?php the_title(); ?>
          </a>  </li>
 <?php endwhile; else: ?>
 <?php endif; ?>
share|improve this question
    
Are you saying you want one list to be "Sports" and the other list to be "Featured Post"? –  Wyck Jan 26 '13 at 0:30
    
I want to see that, When i view category page(e.g Sports),i want to see Featured and Sport(Current Category)post list on sidebar(widget). its worked with my code but my Sport post list too changing(Featured and Sports) i want to see it just on sidebar.. –  Genxer Jan 26 '13 at 0:39
add comment

1 Answer

up vote 1 down vote accepted

I think you're asking about your custom secondary loop affecting your main loop, correct?

query_posts() alters the main loop. A quick fix would be to call wp_reset_query() after your custom loop to reset the query data:

<?php wp_reset_query(); ?>

However, it's more efficient and friendlier to use WP_Query() instead of query_posts() (especially if you're making a secondary loop). This should be the equivalent of your provided code using WP_Query():

<?php
if ( is_category() ) {
    $current_cat = get_query_var('cat');
}

$args = array(
    'category__and' => array( 55, $current_cat )
);

$my_query = new WP_Query( $args );
?>

<?php while ( $my_query->have_posts() ) : $my_query->the_post(); ?>
    <li>
        <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>  
    </li>
<?php endwhile; ?>

<?php wp_reset_postdata(); ?>

References:

http://codex.wordpress.org/Function_Reference/query_posts

http://codex.wordpress.org/Class_Reference/WP_Query

share|improve this answer
    
It's worked.. Many thanx @BigBagel –  Genxer Jan 26 '13 at 0:41
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.