wp_reset_query doesn’t seem to work for two sidebar widgets
-
In my functions.php file, I’m using the following two widgets to display the most recent post from two different categories:
## Latest post function getWords($text, $limit) { $array = explode(" ", $text, $limit+1); if (count($array) > $limit) { unset($array[$limit]); } return implode(" ", $array); } function widget_xx_latestnews1() { $title = 'Latest news'; $r = new WP_Query(array('showposts' => '1', 'what_to_show' => 'posts', 'nopaging' => 0, 'post_status' => 'publish', 'in_category' => '75')); if ($r->have_posts()) : ?> <div class="widget"> <h2><?php echo $title; ?></h2> <?php while ($r->have_posts()) : $r->the_post(); ?> <h3><strong><a href="<?php the_permalink() ?>"><?php if ( get_the_title() ) the_title(); else the_ID(); ?></a></strong></h3> <p class="timestamp"><?php the_time('F j, Y'); ?></p> <p><?php $mycontent = getWords(get_the_content(), 50);?> <?php // Pull the first 30 words ?> <?php echo strip_tags($mycontent); echo"..."; ?> [<a href="<?php the_permalink() ?>" class="title" rel="bookmark" title="<?php the_title(); ?>">read more</a>]</p> <?php endwhile; ?> </div> <?php endif; wp_reset_query(); // Restore global post data stomped by the_post(). }
and then this is the second one, from a different category:
## Featured post function widget_xx_featured1() { wp_reset_query(); $title = 'Featured'; $r = new WP_Query(array('showposts' => '1', 'what_to_show' => 'posts', 'nopaging' => 0, 'post_status' => 'publish', 'in_category' => '79')); if ($r->have_posts()) : ?> <div class="widget"> <h2><?php echo $title; ?></h2> <?php while ($r->have_posts()) : $r->the_post(); ?> <h3><strong><a href="<?php the_permalink() ?>"><?php if ( get_the_title() ) the_title(); else the_ID(); ?></a></strong></h3> <p class="timestamp"><?php the_time('F j, Y'); ?></p> <p><?php $mycontent3 = getWords(get_the_content(), 50);?> <?php // Pull the first 30 words ?> <?php echo strip_tags($mycontent3); echo"..."; ?> [<a href="<?php the_permalink() ?>" class="title" rel="bookmark" title="<?php the_title(); ?>">read more</a>]</p> <?php endwhile; ?> </div> <?php endif; wp_reset_query(); // Restore global post data stomped by the_post(). }
However, both widgets are displaying a post from the category listed in the first one. While the content within the loop is displaying alright, it makes me think that the query isn’t fully being reset. I know enough PHP to break stuff, and that’s about it. ??
Viewing 1 replies (of 1 total)
Viewing 1 replies (of 1 total)
- The topic ‘wp_reset_query doesn’t seem to work for two sidebar widgets’ is closed to new replies.