php - For each loop will not append to the_content hook

I'm using the filter to append items to the content on one page only. It's fully working except for the orderi

I'm using the filter to append items to the content on one page only. It's fully working except for the ordering of the $whole variable. The title is appearing after the content but the for each loop appears above or prepended to the content no matter the order. I think I am passing the loop correctly by using one($arr) but I am still learning php. I'd normally include this type of loop on page template but I'm trying to add it with a plugin.

function insertLoop($content) {

  if( is_page( 'regular-page' ) ) {

  function one( $arr ) {  

    global $post;

    $args = array( 
      'post_type' => 'custom-post-type',
      'posts_per_page' => -1,
    );

   $customposts = get_posts( $args );

   foreach ( $customposts as $post ) : setup_postdata( $post ); ?>

    <div class="custom-post-listing">
        <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
        <p><?php the_date(); ?></p>
    </div>

   <?php endforeach; wp_reset_postdata();

   }

   $words = "<h2>Custom Post Full List</h2>";

   $whole = $content . $title . one($arr);

   return $whole;

  }

}

add_filter ('the_content', 'insertLoop');

I'm using the filter to append items to the content on one page only. It's fully working except for the ordering of the $whole variable. The title is appearing after the content but the for each loop appears above or prepended to the content no matter the order. I think I am passing the loop correctly by using one($arr) but I am still learning php. I'd normally include this type of loop on page template but I'm trying to add it with a plugin.

function insertLoop($content) {

  if( is_page( 'regular-page' ) ) {

  function one( $arr ) {  

    global $post;

    $args = array( 
      'post_type' => 'custom-post-type',
      'posts_per_page' => -1,
    );

   $customposts = get_posts( $args );

   foreach ( $customposts as $post ) : setup_postdata( $post ); ?>

    <div class="custom-post-listing">
        <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
        <p><?php the_date(); ?></p>
    </div>

   <?php endforeach; wp_reset_postdata();

   }

   $words = "<h2>Custom Post Full List</h2>";

   $whole = $content . $title . one($arr);

   return $whole;

  }

}

add_filter ('the_content', 'insertLoop');
Share Improve this question asked Jun 5, 2019 at 3:31 scottie5689scottie5689 12 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

Filter should modify the given value and return it. It should not print anything.

But your foreach loop does exactly opposite - it prints its output and doesn’t append anything to result. So when you use it to append its result to the content, it prints its result and doesn’t return anything - so nothing gets appended.

One way to fix it is to use buffering:

function one( $arr ) {  
    ob_start();
    global $post;

    $args = array( 
      'post_type' => 'custom-post-type',
      'posts_per_page' => -1,
    );

    $customposts = get_posts( $args );

    foreach ( $customposts as $post ) : setup_postdata( $post ); ?>

    <div class="custom-post-listing">
        <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
        <p><?php the_date(); ?></p>
    </div>

    <?php endforeach; wp_reset_postdata();
    return ob_get_clean();
}

Another, a little bit cleaner is to modify this function in such way, that it constructs strings as a result.

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

相关推荐

  • php - For each loop will not append to the_content hook

    I'm using the filter to append items to the content on one page only. It's fully working except for the orderi

    9小时前
    40

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

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

关注微信