C
C
chelnokov_a2020-05-12 12:03:16
PHP
chelnokov_a, 2020-05-12 12:03:16

How to loop through an array of WP posts?

Good dent. Please help solve the problem. I am weak in php from the word at all. I need to display posts on a page. I get this array.

Array (

 [0] => WP_Post Object ( [ID] => 77 [post_author] => 1 [post_date] => 2020-05-11 18:54:02 [post_date_gmt] => 2020-05-11 15:54:02 [post_content] => Очень довольный покупкой мяса для гриля, привезли очень свежее действительно Сын Мясника заботится о покупателях [post_title] => Алекс Мазур [post_excerpt] => [post_status] => publish [comment_status] => closed [ping_status] => closed [post_password] => [post_name] => %d0%b0%d0%bb%d0%b5%d0%ba%d1%81-%d0%bc%d0%b0%d0%b7%d1%83%d1%80 [to_ping] => [pinged] => [post_modified] => 2020-05-11 18:54:02 [post_modified_gmt] => 2020-05-11 15:54:02 [post_content_filtered] => [post_parent] => 0 [guid] => http://xn--80apggebzc1gxb.xn--p1ai/?post_type=otzivi&p=77 [menu_order] => 0 [post_type] => otzivi [post_mime_type] => [comment_count] => 0 [filter] => raw )

 [1] => WP_Post Object ( [ID] => 78 [post_author] => 1 [post_date] => 2020-05-11 18:54:51 [post_date_gmt] => 2020-05-11 15:54:51 [post_content] => Очень довольный покупкой мяса для гриля, привезли очень свежее действительно Сын Мясника заботится о покупателях [post_title] => Алекс Мазур [post_excerpt] => [post_status] => publish [comment_status] => closed [ping_status] => closed [post_password] => [post_name] => %d0%b0%d0%bb%d0%b5%d0%ba%d1%81-%d0%bc%d0%b0%d0%b7%d1%83%d1%80-3 [to_ping] => [pinged] => [post_modified] => 2020-05-11 18:54:51 [post_modified_gmt] => 2020-05-11 15:54:51 [post_content_filtered] => [post_parent] => 0 [guid] => http://xn--80apggebzc1gxb.xn--p1ai/?post_type=otzivi&p=78 [menu_order] => 0 [post_type] => otzivi [post_mime_type] => [comment_count] => 0 [filter] => raw ) 

[2] => WP_Post Object ( [ID] => 79 [post_author] => 1 [post_date] => 2020-05-11 18:54:49 [post_date_gmt] => 2020-05-11 15:54:49 [post_content] => Очень довольный покупкой мяса для гриля, привезли очень свежее действительно Сын Мясника заботится о покупателях [post_title] => Алекс Мазур [post_excerpt] => [post_status] => publish [comment_status] => closed [ping_status] => closed [post_password] => [post_name] => %d0%b0%d0%bb%d0%b5%d0%ba%d1%81-%d0%bc%d0%b0%d0%b7%d1%83%d1%80-2 [to_ping] => [pinged] => [post_modified] => 2020-05-11 18:54:49 [post_modified_gmt] => 2020-05-11 15:54:49 [post_content_filtered] => [post_parent] => 0 [guid] => http://xn--80apggebzc1gxb.xn--p1ai/?post_type=otzivi&p=79 [menu_order] => 0 [post_type] => otzivi [post_mime_type] => [comment_count] => 0 [filter] => raw ) )


Some kind of loop is needed to display the [post_content] and [post_title] fields. Nothing comes out. When I try to iterate through foreach, nothing is rendered. Please tell me how to do it right.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
I
IvanMogilev, 2020-05-12
@IvanMogilev

<?php
foreach ($array as $key)
{
    echo $key->post_title;
    echo $key->post_content;
}

what is that? instead of array is your array name
UPD. Slightly corrected thanks to the comment

D
Dima Polos, 2020-05-12
@dimovich85

global $post;
foreach( $your_array as $post ):
  setup_postdata( $post );
?>
<article>
  <h2> <?php the_title(); ?> </h2>
  <div> <?php the_content(); ?>
</article>
<?php
endforeach;
wp_reset_postdata();

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question