0

I try to push each data from the Wordpress loop into array:

<?php 
$array = array(); 
        $args = -1;
        $posts = get_posts($args);      
        foreach ($posts as $post){
            array_push( $array, array(
                "title" => get_the_title(),
                //capire perchè non stampa il contenuto
                "cont" => get_the_content()
            )); 
        }
        print_r($array);
?>

The problem was that I want to have final data into the multidimensional array but I have only the title value but NOT the content.

Rahil Wazir
  • 10,007
  • 11
  • 42
  • 64
ghost
  • 45
  • 1
  • 5
  • possible duplicate of [array\_push into a multi-dimensional array](http://stackoverflow.com/questions/4870847/array-push-into-a-multi-dimensional-array) – rnevius Oct 13 '14 at 15:05
  • if i replace get_the_content()to the_content() the result is also empty – ghost Oct 13 '14 at 15:14
  • can you post an example please? – ghost Oct 13 '14 at 15:30
  • ok i replace the foreach with the standard Wordpress loop and it work – ghost Oct 13 '14 at 15:34
  • get_the_title(), //capire perchè non stampa il contenuto "cont" => get_the_content() )); } // end while } // end if print_r($array); ?> – ghost Oct 13 '14 at 15:34
  • Good idea, answer your own question though for other users who may be having this problem – Eujinks Oct 13 '14 at 15:35
  • @seanyt123 This will work and it is working. OP only issue is that the content is not being retrieved. See my answer why it didn't – Rahil Wazir Oct 13 '14 at 15:43

1 Answers1

1

Your loop is fine. To access the content get_the_content() you need to use setup_postdata. It sets up the global post data for template tags.

foreach ($posts as $post){
   setup_postdata($post);
   ...
}
Rahil Wazir
  • 10,007
  • 11
  • 42
  • 64