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.

7
  • possible duplicate of array_push into a multi-dimensional array Commented Oct 13, 2014 at 15:05
  • if i replace get_the_content()to the_content() the result is also empty Commented Oct 13, 2014 at 15:14
  • can you post an example please? Commented Oct 13, 2014 at 15:30
  • ok i replace the foreach with the standard Wordpress loop and it work Commented Oct 13, 2014 at 15:34
  • <?php $array = array(); $args = -1; $posts = get_posts($args); if ( have_posts() ) { while ( have_posts() ) { the_post(); array_push( $array, array( "title" => get_the_title(), //capire perchè non stampa il contenuto "cont" => get_the_content() )); } // end while } // end if print_r($array); ?> Commented Oct 13, 2014 at 15:34

1 Answer 1

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);
   ...
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.