Resetting of postdata, still getting same data after second setup_postdata
-
Hi, I’m in the process of transforming static HTML to a WordPress template. I’ve got most things working, but need some help on a subject that’s threatening my sanity ;-).
Due to the way the design has been implemented, I can’t use a foreach-loop to present the stories. I need to fetch them one at a time. At the start of index.php I threfore have
$postar = get_posts(array('numberposts' => 4));
Then I’ve used
setup_postdata($postar[0]);
followed by using the_title() etc and then repeating the process somewhere else in the same file withsetup_postdata($postar[1]);
etc.The problem I’ve got is that everything is perfect after the first setup_postdata, but after the second or third I still get the data from $postar[0], with the exception of the_content() which returns the content from $postar[1]!
To debug, Ive’ added a part:
echo "*Direkte frå objektet:\n" echo $postar[0]->post_title."\n"; echo $postar[1]->post_title."\n"; echo "*Frå wp-funksjonar:\n" setup_postdata($postar[0]); the_title(); setup_postdata($postar[1]); the_title(); ?> echo "*Frå print_r:\n" print_r($postar[0]); print_r($postar[1]);
This returns:
*Direkte frå objektet: Testartikkel1 Hello World! *Frå wp-funksjonar: Testartikkel1 Testartikkel1 *Frå print_r: stdClass Object ( [ID] => 3 [post_author] => 1 [post_date] => 2011-02-27 17:59:46 [post_date_gmt] => 2011-02-27 17:59:46 [post_content] => freopfjrejfre ofjrep fjrep fje rf [post_title] => Testartikkel1 [post_excerpt] => [post_status] => publish [comment_status] => open [ping_status] => open [post_password] => [post_name] => testartikkel1 [to_ping] => [pinged] => [post_modified] => 2011-02-27 18:00:21 [post_modified_gmt] => 2011-02-27 18:00:21 [post_content_filtered] => [post_parent] => 0 [menu_order] => 0 [post_type] => post [post_mime_type] => [comment_count] => 0 [filter] => raw ) stdClass Object ( [ID] => 1 [post_author] => 1 [post_date] => 2011-02-27 17:43:40 [post_date_gmt] => 2011-02-27 17:43:40 [post_content] => Dette er ein testartikkel [post_title] => Hello, World! [post_excerpt] => [post_status] => publish [comment_status] => open [ping_status] => open [post_password] => [post_name] => helloworld [to_ping] => [pinged] => [post_modified] => 2011-02-27 17:43:40 [post_modified_gmt] => 2011-02-27 17:43:40 [post_content_filtered] => [post_parent] => 0 [menu_order] => 0 [post_type] => post [post_mime_type] => [comment_count] => 0 [filter] => raw )
I’ve attempted using wp_reset_postdata(), but that doesn’t help me in any way, still the same result.
- The topic ‘Resetting of postdata, still getting same data after second setup_postdata’ is closed to new replies.