如何解决 the_content 过滤器的问题

How to fix the problem with the_content filter

我想在 the_content 之后添加包含我的自定义 post 信息的简码,但是当我添加它显示在 the_content

之前的代码时

这是我的代码

<?php

function foobar_func() { ?>

  <h3>Title : </h3>
  <ul>
    <li>foo : </li>
  </ul>

<?php }

add_shortcode( 'project_information', 'foobar_func' );

function wpdev_before_after( $content ) {

  $beforecontent = '';
  if ( is_single() ) {
    $aftercontent = '[project_information]';
  } else {
    $aftercontent = '';
  }
  $fullcontent = $beforecontent . $content . $aftercontent;

  return $fullcontent;

}

add_filter('the_content', 'wpdev_before_after');

?>

我的 foobar_func 应该是这样的,我有更多自定义分类法要列出,如果我使用变量来显示它们,我的代码将非常混乱

function foobar_func() { ?>

  <h3>Title : </h3>
  <ul>
    <li>foo : <?php

    $terms = wp_get_post_terms( $post->ID, 'taxonomy', $args );
    foreach( $terms as $term ) {

      if ( end($terms) == $term ){
        echo '<a href="' . esc_url( get_term_link( $term ) ) . '" title="' . esc_attr( sprintf( __( '%s', 'tiads' ), $term->name ) ) . '">' . $term->name . '</a>';
      } else {
        echo '<a href="' . esc_url( get_term_link( $term ) ) . '" title="' . esc_attr( sprintf( __( '%s', 'tiads' ), $term->name ) ) . '">' . $term->name . '</a> , ';
      }

    }

    ?></li>
  </ul>

<?php }

尝试将 html 分配给一个变量并 return 它在您的 foobar_func()

function foobar_func() { 
  $html = "  
  <h3>Title : </h3>
  <ul>
    <li>foo : 
    ";
    $terms = wp_get_post_terms( $post->ID, 'taxonomy', $args );
    foreach( $terms as $term ) {

      if ( end($terms) == $term ){
        $html .= '<a href="' . esc_url( get_term_link( $term ) ) . '" title="' . esc_attr( sprintf( __( '%s', 'tiads' ), $term->name ) ) . '">' . $term->name . '</a>';
      } else {
        $html .= '<a href="' . esc_url( get_term_link( $term ) ) . '" title="' . esc_attr( sprintf( __( '%s', 'tiads' ), $term->name ) ) . '">' . $term->name . '</a> , ';
      }

    }
    $html .= "
    </li>
  </ul>";
  return $html;
}