最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

posts - Foreach issue in get_posts function

programmeradmin5浏览0评论

My category is coming from comma separated place and then i put it into get_posts's args array, but my query gives me only last category's posts. Please have a look at my code below:

$content_arr = explode(",", $content);

  if ( is_array($content_arr) && count($content_arr) > 0 ) {
    foreach ($content_arr as $category_name) { // Category loop
      $args = array(
        'category_name'  => trim($category_name),
        'posts_per_page' => 7,
        'order'          => 'DESC',
        'orderby'        => 'post_date',
      );

      $output = '';      
      // $posts = get_posts($args);
      $post_counter = 1;

      $the_query = new WP_Query( $args );

      if ( $the_query->have_posts() ) : 
        while ( $the_query->have_posts() ) : $the_query->the_post(); 
            if ( has_post_thumbnail() ):
              //the_post_thumbnail();
            else: 

            endif;
            $output .= '<div class="col-md-4 mb-5">'.get_the_ID().'-'.get_the_title().'</div>';
            $post_counter++;
        endwhile; 
        $the_query->reset_postdata();
        echo $post_counter."-";
      endif;

In $content_arr have 3 category as array element so first foreach is running 3 times but my output is showing only one category posts. Somehow it is resetting my previous category's results. How can i show my all categories posts one by one? Thanks.

My category is coming from comma separated place and then i put it into get_posts's args array, but my query gives me only last category's posts. Please have a look at my code below:

$content_arr = explode(",", $content);

  if ( is_array($content_arr) && count($content_arr) > 0 ) {
    foreach ($content_arr as $category_name) { // Category loop
      $args = array(
        'category_name'  => trim($category_name),
        'posts_per_page' => 7,
        'order'          => 'DESC',
        'orderby'        => 'post_date',
      );

      $output = '';      
      // $posts = get_posts($args);
      $post_counter = 1;

      $the_query = new WP_Query( $args );

      if ( $the_query->have_posts() ) : 
        while ( $the_query->have_posts() ) : $the_query->the_post(); 
            if ( has_post_thumbnail() ):
              //the_post_thumbnail();
            else: 

            endif;
            $output .= '<div class="col-md-4 mb-5">'.get_the_ID().'-'.get_the_title().'</div>';
            $post_counter++;
        endwhile; 
        $the_query->reset_postdata();
        echo $post_counter."-";
      endif;

In $content_arr have 3 category as array element so first foreach is running 3 times but my output is showing only one category posts. Somehow it is resetting my previous category's results. How can i show my all categories posts one by one? Thanks.

Share Improve this question edited Jan 30, 2020 at 7:43 Jacob Peattie 44.2k10 gold badges50 silver badges64 bronze badges asked Jan 30, 2020 at 7:37 ashiqueashique 11 bronze badge
Add a comment  | 

1 Answer 1

Reset to default 0

The issue is that in the beginning of each loop you reset $output to an empty string:

$output = '';

So when then loop has finished, $output will only contain the output of the last loop.

To fix the issue you'll just need to move this line outside of the loop, to initialise the variable, while only adding to it with .= inside the loop:

$content_arr = explode( ',', $content);
$output      = '';

if ( is_array( $content_arr ) && count( $content_arr ) > 0 ) {
    foreach ( $content_arr as $category_name ) {
        $args = array(
            'category_name'  => trim( $category_name ),
            'posts_per_page' => 7,
            'order'          => 'DESC',
            'orderby'        => 'post_date',
        );

        $post_counter = 1;
        $the_query    = new WP_Query( $args );

        if ( $the_query->have_posts() ) : 
            while ( $the_query->have_posts() ) : $the_query->the_post(); 
                if ( has_post_thumbnail() ):
                    //the_post_thumbnail();
                else: 

                endif;

                $output .= '<div class="col-md-4 mb-5">' . get_the_ID() . '-' . get_the_title() . '</div>';

                $post_counter++;
            endwhile;

            $the_query->reset_postdata();

            echo $post_counter . "-";
        endif;
发布评论

评论列表(0)

  1. 暂无评论