本文介绍了帖子摘录不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好 stackoverflow 的人,我需要帮助.我正在使用 wordpress,但不知何故我无法使用此功能:

$post_id = 12;echo get_post($post_id)->post_excerpt;

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

 

<div class="inblock1"><h2>关于靴子</h2><p><?php $post_id = 12;echo get_post($post_id)->post_excerpt;?></p><a href="/about-boots/" class="rodykle">apac</a>

解决方案

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

$my_post = get_post($post_id);//如果摘录为空,则从 post_content 生成一个,否则显示保存的摘录回声空($my_post->post_excerpt)?wp_trim_words($my_post->post_content, 55, '...') : $my_post->post_excerpt;

在 Codex 中阅读有关 wp_trim_words() 的更多信息.

Hello stackoverflow people, I need help. I'm using wordpress and somehow I can't use this function:

$post_id = 12;
    echo get_post($post_id)->post_excerpt;

Somehow it prints nothing. Here is the full div. Can you help me to solve this problem?

                <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>
解决方案

It sounds like you don't actually have an excerpt set for this post. You can always use a conditional to test it, and output a custom excerpt (from the post_content) if one doesn't exist:

$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;

Read more about wp_trim_words() in the Codex.

这篇关于帖子摘录不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-10 18:26