[wp-hackers] nested loops in 3.0 trunk

Jean-Nicolas Kuttler wp-hackers at nicolaskuttler.de
Thu May 20 10:50:24 UTC 2010


I've been using the following code for some time to nest a loop into a page's main loop via shortcode.

<?php
function nk_category_cloud( $atts ) {
	extract( shortcode_atts( array(
		'category' => '',
	), $atts ) );
	$r = '';

	$my_query = new WP_Query( "cat=$category" );
	if ( $my_query->have_posts() ) {
		while ( $my_query->have_posts() ) {
			$my_query->the_post();
			$r .= '<div class="nk_category_cloud" >';
			$r .= '<a href="' . get_permalink() . '" title="' . get_the_title() . '" >';
			$r .= get_the_excerpt();
			$r .= '</div>';
			}
		}
	}
	$GLOBALS['post'] =  $GLOBALS['wp_query']->post; // restore global $post
	return $r;
}
add_shortcode( 'nk_category_cloud', 'nk_category_cloud' );
?>

In current trunk $my_query contains all recent posts, not just the ones that are in the right category. The $category parameter is passed correctly by the way. The same code works as expected in 2.9.2.

Any ideas what might be wrong?

Nicolas

--
Nicolas Kuttler
wp at nkuttler.de

http://www.nkuttler.de
http://www.nicolaskuttler.de (deutsch)


More information about the wp-hackers mailing list