使用wp_trim_words和got '注册自定义帖子类型是一个插件领域'在所有的帖子摘录


Using wp_trim_words and got 'registering custom post type is a plugin territory' in all post excerpt

在我的自定义循环中,我使用foreach()在我的一个模板部分显示最新的帖子,称为content-latest.php,在我添加wp_trim_words()显示一些摘录到循环之前,它工作得很好,它的获取文本输出说"注册自定义帖子类型是一个插件领域…"。缩略图,标题是正常的,但不是摘录。the_excerpt()也是如此。

这是我添加摘录之前的代码(和工作,但没有摘录):

    <?php 
    $recent_posts = wp_get_recent_posts();
    foreach( $recent_posts as $recent ){
    if($recent['post_status']=="publish"){
    if ( has_post_thumbnail($recent["ID"])) {
        echo '<li><div class="media">
            <a href="' . get_permalink($recent["ID"]) . '" title="'.esc_attr($recent["post_title"]).'" class="media-left">' 
            .   get_the_post_thumbnail($recent["ID"], 'thumbnail').'</a> <div class="media-body">
            <a href="' . get_permalink($recent["ID"]). '" 
            class="catg_title" style="color:#000000;">' .esc_attr($recent["post_title"]). '</a></div></div></li> ';
      }else{
        //something here.. 
            }
         }
    }
    ?>

代码后:

<?php 
        $recent_posts = wp_get_recent_posts();
        $trim = wp_trim_words( get_the_content(), 7, '...' );
        foreach( $recent_posts as $recent ){
        if($recent['post_status']=="publish"){
        if ( has_post_thumbnail($recent["ID"])) {
            echo '<li><div class="media">
                <a href="' . get_permalink($recent["ID"]) . '" title="'.esc_attr($recent["post_title"]).'" class="media-left">' 
                .   get_the_post_thumbnail($recent["ID"], 'thumbnail').'</a> <div class="media-body">
                <a href="' . get_permalink($recent["ID"]). '" 
                class="catg_title" style="color:#000000;">' .esc_attr($recent["post_title"]). '<br>' . esc_attr($trim) . '</a></div></div></li> ';
          }else{
            //something here.. 
                }
             }
        }
        ?>

注意,我将wp_trim_words分配给$trim并将其称为摘录。我正在学习过程中,所以我不知道为什么或如何处理这个。有人能给我解释一下吗?

get_the_content需要一个post id才能工作

foreach( $recent_posts as $recent ){
   $trim = wp_trim_words( get_the_content($recent['ID']), 7, '...' );
}