2015-09-16 110 views
1

你好,stackoverflow的人,我需要帮助。我正在使用wordpress,不知何故我不能使用此功能:帖子摘录不工作

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

不知何故,它不打印任何东西。这是完整的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> 
+4

你确定数据库中的'post_excerpt'列不是空的吗? –

+1

'post_excerpt'属性是指数据库中的摘录字段 - 所以如果你没有手动设置它是空的。 – vard

+0

有一个[wordpress stack exchange](http://wordpress.stackexchange.com/)网站专门针对这些类型的问题。如果这没有帮助,你可能会有更好的运气。 –

回答

5

这听起来像你实际上没有为这篇文章设置摘录。您可以随时使用条件进行测试,并输出一个自定义的摘录(从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; 

了解更多关于wp_trim_words() in the Codex

3

您可能需要使用setup_postdata()因为<?php $post_id = 12; echo get_post($post_id)->post_excerpt; ?>将返回摘录,如果你有摘录领域,但低于代码摘录数据将返回从内容的数据,如果你没有在摘录现场数据。

$post_id = 12; 
$tempVar = $post; 
$post = get_post($post_id); 
setup_postdata($post); 

the_excerpt(); 

wp_reset_postdata(); 
$post = $tempVar; 
+0

什么都没有 - \t \t \t \t \t'

<?php $ post_id = 12; $ excerpt = apply_filters('the_excerpt',get_post_field('post_excerpt',$ post_id)); echo $ excerpt; ?>

' – McLaren

+0

你不应该在'the_excerpt()'中使用'echo'。 – rnevius