在循环
-
-
好的,使用my_excerpt($post->post_content,get_the_excerpt())并使用http://wordpress.stackexchange.com/questions/6961/using-wp-trim-excerpt的my_excerpt()函数得到它获得摘录的循环外ok, got it using `my_excerpt($post->post_content, get_the_excerpt())` and using the `my_excerpt()` function from http://wordpress.stackexchange.com/questions/6961/using-wp-trim-excerpt-to-get-the-excerpt-outside-the-loop
- 0
- 2011-08-24
- ariel
-
请添加您想出的解决方案作为答案,因此这不会困扰网站作为未解决的问题.:)Please add solution you came up with as an answer, so this doesn't haunt site as unanswered question. :)
- 3
- 2011-09-11
- Rarst
-
在调用`get_the_excerpt()`之前,只需使用`the_post()`(它也适用于单个帖子模板)函数,它将为您设置必要的数据.Just use `the_post()` (it works on single post template too) function before you call `get_the_excerpt()` it will setup necessary data for you.
- 0
- 2014-09-18
- Sisir
-
9 个回答
- 投票数
-
- 2011-09-13
使用
my_excerpt($post->post_content,get_the_excerpt())
并使用使用wp_trim_excerpt在循环外获取the_excerpt()got it using
my_excerpt($post->post_content, get_the_excerpt())
and using themy_excerpt()
function from Using wp_trim_excerpt to get the_excerpt() outside the loop-
仅链接的答案不好.在此处复制相关代码.当该链接断开时,该站点已关闭/消失,那么此答案没有任何价值.Link-only answers are no good. Copy the relevant code here. When that link is broken, that site is down / gone, then this answer has no value.
- 2
- 2014-06-18
- random_user_name
-
它对我来说很完美!It worked perfectly for me!
- 0
- 2017-07-24
- Saikat
-
- 2014-06-18
我在寻找如何在没有发布对象的情况下执行此操作时发现了这个问题.
我的其他研究发现了这种巧妙的技术:
$text = apply_filters('the_excerpt', get_post_field('post_excerpt', $post_id));
I found this question when looking how to do this without the post object.
My additional research turned up this slick technique:
$text = apply_filters('the_excerpt', get_post_field('post_excerpt', $post_id));
-
应该将其作为答案,因为这是将数据拉出循环的推荐方法.也不需要任何自定义功能或$post全局变量的覆盖.This should be accepted as answer as it's the recommended way to pull data outside the loop. Also doesn't require any custom function or overriding of the `$post` global variable.
- 1
- 2015-06-16
- MacK
-
它返回空字符串.it return empty string.
- 4
- 2016-01-20
- Kyaw Tun
-
@KyawTun-它有效,只要设置了$post_id($post_id的值是什么?而$post_id是有效的合法帖子ID).@KyawTun - it works, so long as `$post_id` is set (what is the value of `$post_id`? AND `$post_id` is a valid, legitimate post ID.
- 1
- 2016-01-20
- random_user_name
-
@cale_b谢谢.我使用get_posts查询并从结果数组中获取ID.post对象确实具有post_title,post_content,ID等.但不起作用.@cale_b Thanks. I use get_posts query and get ID from the resulting array. The post object does have post_title, post_content, ID, etc. But not working.
- 2
- 2016-01-21
- Kyaw Tun
-
如果只需要TEXT而不是the_excerpt过滤器随附的
标记,则使用"get_the_excerpt"过滤器,这样上面的过滤器将变为:$text=apply_filters('get_the_excerpt',get_post_field('post_excerpt',$post_id));这只会提供您可以在自己的标记中的任何位置插入的RAW文本.
If you need JUST the TEXT and nottag which is included with the_excerpt filter, then use "get_the_excerpt" filter, so that above filter becomes: $text = apply_filters('get_the_excerpt', get_post_field('post_excerpt', $post_id)); this will give you just the RAW text you can insert anywhere in your own markup.
- 0
- 2016-05-20
- Mohsin
-
也不适合我.请记住:"帖子的摘录.这可以是用户提供的摘录,原样返回,也可以是自动生成的单词计数的完整帖子内容的精简版本."可以为用户提供的产品工作吗?我正在寻找自动生成的摘录.Doesn't work for me either. Remember: "the excerpt of the post. This is either a user-supplied excerpt, that is returned unchanged, or an automatically generated word-counted trimmed-down version of the full post content." Might work for the user-supplied one? I was looking for the automatically-generated excerpt.
- 0
- 2019-03-20
- Fabien Snauwaert
-
- 2012-06-08
由于似乎您已经有需要摘录的post对象,因此您可以强制工作:
setup_postdata( $post ); $excerpt = get_the_excerpt();
setup_postdata()
函数将全球化$post
对象,并使其可用于常规的旧循环函数.在循环中时,您调用the_post()
并为您进行设置...在循环外,您需要手动强制执行.Since it seems you already have the post object you need the excerpt for, you can just force things to work:
setup_postdata( $post ); $excerpt = get_the_excerpt();
The
setup_postdata()
function will globalize the$post
object and make it available for regular old loop function. When you're inside the loop, you callthe_post()
and it sets things up for you ... outside of the loop you need to force it manually.-
可以,但是:"您必须传递对全局`$post`变量的引用,否则`the_title()`之类的函数将无法正常工作." 全局$post; $post=$post_object; setup_postdata($post); $excerpt=get_the_excerpt();`This works but: "You must pass a reference to the global `$post` variable, otherwise functions like `the_title()` don't work properly." `global $post;$post = $post_object;setup_postdata( $post );$excerpt = get_the_excerpt();`
- 1
- 2017-01-19
- deach
-
setup_postdata($post); FTW !!!!`setup_postdata($post);` FTW!!!!
- 0
- 2017-04-30
- squarecandy
-
- 2012-06-08
尝试一下:
在functions.php中创建一个新函数,然后从任何地方调用它.
function get_excerpt_by_id($post_id){ $the_post = get_post($post_id); //Gets post ID $the_excerpt = $the_post->post_content; //Gets post_content to be used as a basis for the excerpt $excerpt_length = 35; //Sets excerpt length by word count $the_excerpt = strip_tags(strip_shortcodes($the_excerpt)); //Strips tags and images $words = explode(' ', $the_excerpt, $excerpt_length + 1); if(count($words) > $excerpt_length) : array_pop($words); array_push($words, '…'); $the_excerpt = implode(' ', $words); endif; $the_excerpt = '<p>' . $the_excerpt . '</p>'; return $the_excerpt; }
Try this:
Create a new function in functions.php and then call it from wherever.
function get_excerpt_by_id($post_id){ $the_post = get_post($post_id); //Gets post ID $the_excerpt = $the_post->post_content; //Gets post_content to be used as a basis for the excerpt $excerpt_length = 35; //Sets excerpt length by word count $the_excerpt = strip_tags(strip_shortcodes($the_excerpt)); //Strips tags and images $words = explode(' ', $the_excerpt, $excerpt_length + 1); if(count($words) > $excerpt_length) : array_pop($words); array_push($words, '…'); $the_excerpt = implode(' ', $words); endif; $the_excerpt = '<p>' . $the_excerpt . '</p>'; return $the_excerpt; }
-
很好找到我的朋友.我从未理解过WordPress为什么会弃用如此重要的功能.这实际上是从头开始重建它,但是它可以工作.考虑到我们经常使用社交共享插件等功能在循环外使用摘录,它可能应该仍然是核心的一部分.Excellent find my friend. I've never understood why WordPress would have deprecated such a critical function. This is effectively rebuilding it from scratch, but it works. Given how often we use the excerpt outside the loop with features like social sharing plugins, it probably should have remained a part of the core.
- 1
- 2014-05-15
- Imperative Ideas
-
EAMann的答案是解决此问题的更好方法,应将其视为最佳实践.这种方法基本上是在复制Core的内部结构,而不是使用API.EAMann's answer is a much better approach to this problem, and the should be considered the best practice. This approach is basically duplicating Core's internals instead of using the API.
- 1
- 2015-03-24
- Ian Dunn
-
- 2016-05-20
现在,您只需使用
get_the_excerpt( $postID )
函数. 从以下版本开始:WordPress 4.5.0引入了$post
参数.Now you can simply use the
get_the_excerpt( $postID )
function. Since: WordPress 4.5.0 introduced the$post
parameter.-
由于我们处于WP 4.5 +时代,因此这应该是新的接受的答案.This should be new accepted answer since we are in WP 4.5 + era.
- 1
- 2016-06-13
- Matija Mrkaic
-
如果摘录为空,这将不起作用,因为wp_trim_excerpt过滤器将返回当前帖子的摘录.This won't work if the excerpt is empty as the `wp_trim_excerpt` filter will return the excerpt for the current post.
- 18
- 2016-08-16
- Dylan
-
有关@Dylan所说的内容的详细信息,请参阅https://core.trac.wordpress.org/ticket/36934See https://core.trac.wordpress.org/ticket/36934 for details on what @Dylan said
- 9
- 2016-09-14
- kraftner
-
- 2012-11-25
如果没有post对象,这是一个简短的功能,如Withers的功能.
function get_excerpt_by_id($post_id){ $the_post = get_post($post_id); $the_excerpt = $the_post->post_excerpt; return $the_excerpt; }
In case you don't have the post object, here's a short function like the one from Withers.
function get_excerpt_by_id($post_id){ $the_post = get_post($post_id); $the_excerpt = $the_post->post_excerpt; return $the_excerpt; }
-
- 2014-09-30
这是当您想在循环外使用
get_the_excerpt()
时使用的:function custom_get_excerpt($post_id) { $temp = $post; $post = get_post($post_id); setup_postdata($post); $excerpt = get_the_excerpt(); wp_reset_postdata(); $post = $temp; return $excerpt; }
This is for when you want to use
get_the_excerpt()
outside the loop:function custom_get_excerpt($post_id) { $temp = $post; $post = get_post($post_id); setup_postdata($post); $excerpt = get_the_excerpt(); wp_reset_postdata(); $post = $temp; return $excerpt; }
-
这是最直接的方法.虽然不确定它在性能上是否很好.你仍然得到我的+1This is the most direct way to do it.. Not sure it's great performance-wise though. You still get my +1
- 0
- 2014-11-07
- Bill
-
- 2017-05-15
如果您想自动从一行内容中摘录-您可以使用
wp_trim_words
函数如下:// 30 is the number of words ehere $excerpt = wp_trim_words(get_post_field('post_content', $post_id), 30);
If you'd like to generate the excerpt automatically from the content in one line - you can use
wp_trim_words
function like this:// 30 is the number of words ehere $excerpt = wp_trim_words(get_post_field('post_content', $post_id), 30);
-
-
请** [编辑]您的答案**,并添加说明:**为什么**可以解决问题?Please **[edit] your answer**, and add an explanation: **why** could that solve the problem?
- 0
- 2018-03-14
- fuxia
-
我有一个调用
get_the_title()
的代码,它可以工作,但是get_the_excerpt()
返回空.我该如何运作?此代码位于名为" WP Facebook Open Graph协议"的插件中.这是我要更改的部分:
此处,
has_excerpt
总是失败,并且get_the_excerpt($post->ID)
不再起作用(已弃用).那么,如何在其中显示摘录?
ps:我也在使用"高级摘录"插件