在本规范范围内,$post 未定义。为了使用$post global,您需要事先将其声明为global(通常在使用它的函数体的顶部):
global $post;
$id = $post->ID;
也就是说,通常不鼓励直接与globals交互,因为您经常访问WordPress的原始状态数据,很可能会绕过重要的挂钩或逻辑。如果可能,请使用WordPress提供的接口来访问此数据:
$id = get_the_ID();
退房
the documentation for the get_terms() function - 它只需要一个参数,就可以全局查询术语,而不需要考虑post ID。我认为您打算使用这些调用
the wp_get_post_terms() 功能。
如果此筛选器仅用于更改podcast 文章类型,您也应该在修改摘录之前检查文章类型。
WordPress的过滤器旨在接收一个值作为参数,并返回其修改或未修改的版本-在过滤器中打印输出通常是个坏主意,可能会导致意外的错误或行为。虽然在大字符串中编写标记可能很烦人,但有一个方便的解决方案,可以在;输出缓冲区;然后将缓冲区的内容转储到字符串。
考虑到以上所有因素,我可能会将此过滤器重写为:
add_filter( \'the_excerpt\', function( $excerpt ) {
  if( get_post_type() !== \'podcast\' )
    return $excerpt;
  
  $themes = wp_get_post_terms(
    get_the_ID(),
    \'themes\',
     array(
       \'hide_empty\' => false,
       \'orderby\'    => \'name\',
       \'order\'      => \'ASC\'
     )
   );
   $guests = wp_get_post_terms(
     get_the_ID(),
     \'guests\',
     array(
       \'hide_empty\' => false,
       \'orderby\'    => \'name\',
       \'order\'      => \'ASC\'
     )
   );
   ob_start();
  ?> 
    <p>
    <?php foreach( $themes as $theme ): ?>    
      <span class="theme"><?php echo $theme->name; ?></span>
    <?php endforeach;?>
    </p>
    <div class="guest-wrap"><span class="guest-label">Episode Guests: </span>
    <?php foreach($guests as $guest ): ?>  
      <span class="guests"><?php echo $guest->name; ?></span>
    <?php endforeach; ?>
    </div>
  <?php
  $term_markup = ob_get_clean();
  $player      = get_post_meta( get_the_ID(), \'embed_episode_code\', true );
   
  return $term_markup . $player . $excerpt;
} );