wordpress 通过 id 获取摘录

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11370599/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 18:58:07  来源:igfitidea点击:

wordpress get excerpt by id

wordpress

提问by Neros

I'm using the options framework

我正在使用选项框架

and i can't work out why this doesnt work

我不知道为什么这不起作用

$x = of_get_option('post_number');
$content_post = get_post($x);
echo $content_post->post_excerpt;

its very odd because

这很奇怪,因为

echo of_get_option('post_number');

works perfectly and outputs a number

完美运行并输出一个数字

and according to get_postmy code should work yet the echo produces nothing, not even an error message

并且根据get_post我的代码应该可以工作但回声什么也不产生,甚至没有错误消息

so i must be handeling get_post() incorrectly somehow, any clues?

所以我必须以某种方式错误地处理 get_post() ,有什么线索吗?



EDIT

编辑

var dump http://pastebin.com/ZEgQ5WPnreveals that post_content is full but post_excerpt is empty

var dump http://pastebin.com/ZEgQ5WPn显示 post_content 已满但 post_excerpt 为空

how do i regenerate the excerpt?

我如何重新生成摘录?



EDIT [resolved]

编辑 [已解决]

i decided to manualy overwrite the excerpt but my option was missing, then i found this

我决定手动覆盖摘录,但我的选项丢失了,然后我找到了这个

and used

并使用

add_post_type_support( 'page', 'excerpt' );

to manualy write the excerpt

手动编写摘录

回答by realmag777

$text = apply_filters('the_excerpt', get_post_field('post_excerpt', $post_id));

回答by heathhettig

This will take the post_content and create an excerpt out of it. You can substitute the post_content for any other string of code. Change the 55 to another number to increase the amount of words returned.

这将获取 post_content 并从中创建一个摘录。您可以将 post_content 替换为任何其他代码字符串。将 55 更改为另一个数字以增加返回的字数。

$excerpt = wp_trim_words ( strip_shortcodes( $recent["post_content"], 55 ) );

回答by buley

You should be able to use get_post()like this, which returnsalmost all built-in post attributes as part of the post object.

你应该能够像这样使用get_post(),它返回几乎所有内置的 post 属性作为 post 对象的一部分。

<?php
  $my_id = 7;
  $my_post = get_post( $my_id ); 
  $my_excerpt = $my_post->post_excerpt;
  var_dump( $my_excerpt );
?> 

If that fails (it shouldn't, but perhaps you've tried by the sound of it) maybe checkout out WP_Queryand pass in "p=$my_id"as a param. This is likely the function used under the get_post hood anyways.

如果失败(它不应该,但也许你已经尝试过它的声音)可能签出WP_Query"p=$my_id"作为参数传入。无论如何,这很可能是在 get_post 引擎盖下使用的函数。

<?php
  $my_id = 7;
  $my_posts = new WP_Query( "p=$my_id" ); 
  var_dump( $my_posts );
?>