帖子摘录不起作用

McL*_*ren 1 php wordpress

你好stackoverflow人,我需要帮助.我正在使用wordpress,不知怎的,我不能使用这个功能:

$post_id = 12;
    echo get_post($post_id)->post_excerpt;
Run Code Online (Sandbox Code Playgroud)

不知何故它什么都不打印.这是完整的div.你能帮我解决这个问题吗?

                <div id="block1">
                    <div class="inblock1">
                        <h2>About boots</h2>
                        <p><?php $post_id = 12;
echo get_post($post_id)->post_excerpt; ?> </p>
                        <a href="/about-boots/" class="rodykle">apac</a>
                    </div>
                </div>
Run Code Online (Sandbox Code Playgroud)

rne*_*ius 6

听起来你实际上并没有为这篇文章设置摘录.您始终可以使用条件对其进行测试,并输出自定义摘录(post_content如果不存在):

$my_post = get_post($post_id);
// If the excerpt is empty, generate one from post_content, else display the saved excerpt
echo empty($my_post->post_excerpt) ? wp_trim_words($my_post->post_content, 55, '...') : $my_post->post_excerpt;
Run Code Online (Sandbox Code Playgroud)

阅读有关wp_trim_words()食典委的更多信息.