简体   繁体   中英

Foreach is showing only one result instead of all

I'm trying to select elements from only one category and show them on page. Currently I have 5 posts in this category but on page I see only one.

Why is that?

Here is how I try

<?php
$args = array(   
    'showposts'=>-1,
    'category_name' => 'custom-page',
); 
$query = new WP_Query( $args ); 
$aSolutionsePost = array();
if ( $query->have_posts() ) {
        while ( $query->have_posts() ) {
            $aSolutionsePost = array();
            $query->the_post();
            $aSolutionsePost['title'] = $query->post->post_title;
            $aSolutionsePost['content'] = $query->post->post_content;
        }
}
?>

<div class="col-4 ">
    <ul class="price">
    <?php if(!empty($aSolutionsePost)){?>

        <?php foreach($aSolutionsePost as $item){ ?>
        <li class="header"><?php echo $item->title; ?></li>
        <li class="grey"><?php echo apply_filters('the_content',$item->content);?></li>
        <?php }?>               
    <?php }?>    
    </ul>
</div>

When I print_r($aSolutionsePost); I see only one result. This:

Array ( 
      [title] => Price test title 
      [content] => Price test content 

      [0] => Array ( 
             [title] => Price test title 
             [content] => Price test content 
      ) 
)

This line of code inside your while ( $query->have_posts() ) loop:

$aSolutionsePost = array();

is overwriting the value of $aSolutionsePost each pass through the loop. You probably want something like this instead:

while ( $query->have_posts() ) {
    $query->the_post();
    $aSolutionsePost[] = array('title' => $query->post->post_title,
                               'content' => $query->post->post_content);
}

Note that in your code to echo the results, you are treating the array elements as objects, not associative arrays. It's simplest to just change those lines to this:

<li class="header"><?php echo $item['title']; ?></li>
<li class="grey"><?php echo apply_filters('the_content',$item['content']);?></li>

but if you want to keep that code the same you can change the assignment line to this:

$aSolutionsePost[] = (object)array('title' => $query->post->post_title,
                                   'content' => $query->post->post_content);

Check array set inside while,

You re-create $aSolutionsePost every time, so it will be reseted. Solution is to create new array and append it to $aSolutionsePost , check below snippet.

while ( $query->have_posts() ) {
    $tmpSolutionsePost = array();
    $query->the_post();
    $tmpSolutionsePost['title'] = $query->post->post_title;
    $tmpSolutionsePost['content'] = $query->post->post_content;
    $aSolutionsePost[] = $tmpSolutionsePost;
}

You are actually updating the same array with new values in the loop.

$finalData =array(); 
 while ( $query->have_posts() ) {
            $aSolutionsePost = array();
            $query->the_post();
            $aSolutionsePost['title'] = $query->post->post_title;
            $aSolutionsePost['content'] = $query->post->post_content;
            $finalData[] = $aSolutionsePost;
        }

Now iterate over $finalData .

This is because you are adding the element to same key of array again and again and that is why there is only 1 element in the array, hence foreach is showing only 1 element. You should possibly take 2d array to solve this purpose.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM