0

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; ?>
2
  • Are you saying you want one list to be "Sports" and the other list to be "Featured Post"?
    – Wyck
    Commented Jan 26, 2013 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
    Commented Jan 26, 2013 at 0:39

1 Answer 1

1

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

1
  • It's worked.. Many thanx @BigBagel
    – Genxer
    Commented Jan 26, 2013 at 0:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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