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

php - For each loop will not append to the_content hook

programmeradmin2浏览0评论

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.

发布评论

评论列表(0)

  1. 暂无评论