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:Read more about
wp_trim_words()
in the Codex.You may need to use
setup_postdata()
because<?php $post_id = 12; echo get_post($post_id)->post_excerpt; ?>
will return excerpt if you have excerpt data in excerpt field but below code will return data from content if you don't have data in excerpt field.