WordPress.org

Ready to get started?Download WordPress

Forums

wp_reset_query doesn't seem to work for two sidebar widgets (2 posts)

  1. randomdeanna
    Member
    Posted 5 years ago #

    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. ;-)

  2. randomdeanna
    Member
    Posted 5 years ago #

    For the record, not knowing why this works, but I changed the second widget to not use the array:

    $r = new WP_Query("showposts=1&amp;cat=79");

    and it worked. Who knew?

Topic Closed

This topic has been closed to new replies.

About this Topic