posts - Foreach issue in get_posts function

My category is coming from comma separated place and then i put it into get_posts's args array, but my query gives

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;

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744790631a4593894.html

相关推荐

  • posts - Foreach issue in get_posts function

    My category is coming from comma separated place and then i put it into get_posts's args array, but my query gives

    10小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信