使用wp_trim_excerpt将the_excerpt()移出循环
-
-
您可以尝试调用摘录过滤器...`$myvar=apply_filters('the_excerpt',$myvar);`You could try calling the excerpt filters... `$myvar = apply_filters( 'the_excerpt', $myvar );`
- 0
- 2011-01-15
- t31os
-
7 个回答
- 投票数
-
- 2012-03-22
自WP 3.3.0起,如果您能够获取要为其生成摘录的内容,则
wp_trim_words()
将很有帮助.希望这对某人有帮助,并且省去了创建自己的单词计数功能的麻烦.Since WP 3.3.0,
wp_trim_words()
is helpful if you're able to get the content that you want to generate an excerpt for. Hope that's helpful to someone and it saves creating your own word counting function. -
- 2011-01-14
wp_trim_excerpt()
有一些奇怪的机制-如果有任何东西传递给它,那么它什么也不做.这是背后的基本逻辑:
-
get_the_excerpt()
检查手动摘录; -
wp_trim_excerpt()
如果没有手动摘录并从内容或预告片中摘录,则会发出提示.
两者与全局变量紧密相关,因此Loop.
在循环之外,最好从
wp_trim_excerpt()
中取出代码并编写自己的修整函数.wp_trim_excerpt()
has a little curious mechanics - if anything is passed to it then it does nothing.Here is basic logic behind it:
get_the_excerpt()
checks for manual excerpt;wp_trim_excerpt()
chimes in if there is no manual excerpt and makes one from content or teaser.
Both are tightly tied to global variables and so Loop.
Outside the Loop you are better of taking code out of
wp_trim_excerpt()
and writing your own trim function. -
- 2011-01-21
更新:
这是我使用的wp_trim_excerpt()的派生词.完美运作.源自Wordpress版本3.0.4
function my_excerpt($text, $excerpt) { if ($excerpt) return $excerpt; $text = strip_shortcodes( $text ); $text = apply_filters('the_content', $text); $text = str_replace(']]>', ']]>', $text); $text = strip_tags($text); $excerpt_length = apply_filters('excerpt_length', 55); $excerpt_more = apply_filters('excerpt_more', ' ' . '[...]'); $words = preg_split("/[\n\r\t ]+/", $text, $excerpt_length + 1, PREG_SPLIT_NO_EMPTY); if ( count($words) > $excerpt_length ) { array_pop($words); $text = implode(' ', $words); $text = $text . $excerpt_more; } else { $text = implode(' ', $words); } return apply_filters('wp_trim_excerpt', $text, $raw_excerpt); }
Update:
Here is a derivative of wp_trim_excerpt() which I used. Works perfectly. Derived from Wordpress version 3.0.4
function my_excerpt($text, $excerpt) { if ($excerpt) return $excerpt; $text = strip_shortcodes( $text ); $text = apply_filters('the_content', $text); $text = str_replace(']]>', ']]>', $text); $text = strip_tags($text); $excerpt_length = apply_filters('excerpt_length', 55); $excerpt_more = apply_filters('excerpt_more', ' ' . '[...]'); $words = preg_split("/[\n\r\t ]+/", $text, $excerpt_length + 1, PREG_SPLIT_NO_EMPTY); if ( count($words) > $excerpt_length ) { array_pop($words); $text = implode(' ', $words); $text = $text . $excerpt_more; } else { $text = implode(' ', $words); } return apply_filters('wp_trim_excerpt', $text, $raw_excerpt); }
-
您不必发布新答案,您可以随时编辑旧答案以包括新信息.例如,您可以将第一个答案中指向WP代码的链接复制到该答案中,然后删除您的第一个答案.You don't have to post a new answer, you can always edit your old one to include new information. You could, for example, copy the link to the WP code from your first answer into this one and then delete your first answer.
- 0
- 2011-01-25
- Jan Fabry
-
对于复制/粘贴:添加$ raw_excerpt=$text;For copy/pasters out there: add $raw_excerpt = $text;
- 0
- 2015-04-11
- Svetoslav Marinov
-
- 2011-08-26
这是我对以帖子对象或帖子ID为参数的"trim_excerpt"的看法.
显然是基于核心内容.不知道为什么这个(和get_the_author())没有非循环等效项.
/** * Generates an excerpt from the content, if needed. * * @param int|object $post_or_id can be the post ID, or the actual $post object itself * @param string $excerpt_more the text that is applied to the end of the excerpt if we algorithically snip it * @return string the snipped excerpt or the manual excerpt if it exists */ function zg_trim_excerpt($post_or_id, $excerpt_more = ' [...]') { if ( is_object( $post_or_id ) ) $postObj = $post_or_id; else $postObj = get_post($post_or_id); $raw_excerpt = $text = $postObj->post_excerpt; if ( '' == $text ) { $text = $postObj->post_content; $text = strip_shortcodes( $text ); $text = apply_filters('the_content', $text); $text = str_replace(']]>', ']]>', $text); $text = strip_tags($text); $excerpt_length = apply_filters('excerpt_length', 55); // don't automatically assume we will be using the global "read more" link provided by the theme // $excerpt_more = apply_filters('excerpt_more', ' ' . '[...]'); $words = preg_split("/[\n\r\t ]+/", $text, $excerpt_length + 1, PREG_SPLIT_NO_EMPTY); if ( count($words) > $excerpt_length ) { array_pop($words); $text = implode(' ', $words); $text = $text . $excerpt_more; } else { $text = implode(' ', $words); } } return apply_filters('wp_trim_excerpt', $text, $raw_excerpt); }
Here's my take on a "trim_excerpt" that takes the post object or a post ID as a parameter.
Obviously based on what's in core. Don't know why this (and get_the_author()) don't have non-loop equivalents.
/** * Generates an excerpt from the content, if needed. * * @param int|object $post_or_id can be the post ID, or the actual $post object itself * @param string $excerpt_more the text that is applied to the end of the excerpt if we algorithically snip it * @return string the snipped excerpt or the manual excerpt if it exists */ function zg_trim_excerpt($post_or_id, $excerpt_more = ' [...]') { if ( is_object( $post_or_id ) ) $postObj = $post_or_id; else $postObj = get_post($post_or_id); $raw_excerpt = $text = $postObj->post_excerpt; if ( '' == $text ) { $text = $postObj->post_content; $text = strip_shortcodes( $text ); $text = apply_filters('the_content', $text); $text = str_replace(']]>', ']]>', $text); $text = strip_tags($text); $excerpt_length = apply_filters('excerpt_length', 55); // don't automatically assume we will be using the global "read more" link provided by the theme // $excerpt_more = apply_filters('excerpt_more', ' ' . '[...]'); $words = preg_split("/[\n\r\t ]+/", $text, $excerpt_length + 1, PREG_SPLIT_NO_EMPTY); if ( count($words) > $excerpt_length ) { array_pop($words); $text = implode(' ', $words); $text = $text . $excerpt_more; } else { $text = implode(' ', $words); } } return apply_filters('wp_trim_excerpt', $text, $raw_excerpt); }
-
- 2011-01-21
+1到Rast.非常奇怪的是,在很明显应该没有get_the_excerpt($post-> ID)之类的东西的情况下.无论如何,这是wordpress版本3.0.4中的wp_trim_excerpt():
http://core.trac. wordpress.org/browser/tags/3.0.4/wp-includes/formatting.php
function wp_trim_excerpt($text) { 1824 $raw_excerpt = $text; 1825 if ( '' == $text ) { 1826 $text = get_the_content(''); 1827 1828 $text = strip_shortcodes( $text ); 1829 1830 $text = apply_filters('the_content', $text); 1831 $text = str_replace(']]>', ']]>', $text); 1832 $text = strip_tags($text); 1833 $excerpt_length = apply_filters('excerpt_length', 55); 1834 $excerpt_more = apply_filters('excerpt_more', ' ' . '[...]'); 1835 $words = preg_split("/[\n\r\t ]+/", $text, $excerpt_length + 1, PREG_SPLIT_NO_EMPTY); 1836 if ( count($words) > $excerpt_length ) { 1837 array_pop($words); 1838 $text = implode(' ', $words); 1839 $text = $text . $excerpt_more; 1840 } else { 1841 $text = implode(' ', $words); 1842 } 1843 } 1844 return apply_filters('wp_trim_excerpt', $text, $raw_excerpt); 1845 }
您可以在第1826行上看到它通过get_the_contents链接到$post全局变量.是的,我不知道他们在想什么.但是从这里,在您自己的my_excerpt中将get_the_content替换为$text,它的行为应类似.
+1 to Rast. It is very weird that there is no such thing as get_the_excerpt($post->ID), when it should be quite obvious that it should. Anyway, here is wp_trim_excerpt() in wordpress version 3.0.4:
http://core.trac.wordpress.org/browser/tags/3.0.4/wp-includes/formatting.php
function wp_trim_excerpt($text) { 1824 $raw_excerpt = $text; 1825 if ( '' == $text ) { 1826 $text = get_the_content(''); 1827 1828 $text = strip_shortcodes( $text ); 1829 1830 $text = apply_filters('the_content', $text); 1831 $text = str_replace(']]>', ']]>', $text); 1832 $text = strip_tags($text); 1833 $excerpt_length = apply_filters('excerpt_length', 55); 1834 $excerpt_more = apply_filters('excerpt_more', ' ' . '[...]'); 1835 $words = preg_split("/[\n\r\t ]+/", $text, $excerpt_length + 1, PREG_SPLIT_NO_EMPTY); 1836 if ( count($words) > $excerpt_length ) { 1837 array_pop($words); 1838 $text = implode(' ', $words); 1839 $text = $text . $excerpt_more; 1840 } else { 1841 $text = implode(' ', $words); 1842 } 1843 } 1844 return apply_filters('wp_trim_excerpt', $text, $raw_excerpt); 1845 }
You can see on line 1826 that it is linked to the $post global variable via get_the_contents. And yes, I have no idea what were they thinking. But from here, replace the get_the_content with $text in your own my_excerpt, and it should behave in a similar fashion.
-
- 2011-04-17
如果$more!=0,则get_the_content()函数将返回完整内容. 您必须将全局变量$more设置为0,以确保get_the_content()函数返回摘录.
修改后的wp_trim_excerpt()函数:
function wp_trim_excerpt($text) { $raw_excerpt = $text; if ( '' == $text ) { global $more; $tmp = $more; $more = 0; $text = get_the_content(''); $more = $tmp; $text = strip_shortcodes( $text ); $text = apply_filters('the_content', $text); $text = str_replace(']]>', ']]>', $text); $text = strip_tags($text); $excerpt_length = apply_filters('excerpt_length', 55); $excerpt_more = apply_filters('excerpt_more', ' ' . '[...]'); $words = preg_split("/[\n\r\t ]+/", $text, $excerpt_length + 1, PREG_SPLIT_NO_EMPTY); if ( count($words) > $excerpt_length ) { array_pop($words); $text = implode(' ', $words); $text = $text . $excerpt_more; } else { $text = implode(' ', $words); } } return apply_filters('wp_trim_excerpt', $text, $raw_excerpt); }
The get_the_content() function would return full content if $more != 0. You have to set global variable $more to 0 to make sure get_the_content() function return excerpt.
Modified wp_trim_excerpt() function:
function wp_trim_excerpt($text) { $raw_excerpt = $text; if ( '' == $text ) { global $more; $tmp = $more; $more = 0; $text = get_the_content(''); $more = $tmp; $text = strip_shortcodes( $text ); $text = apply_filters('the_content', $text); $text = str_replace(']]>', ']]>', $text); $text = strip_tags($text); $excerpt_length = apply_filters('excerpt_length', 55); $excerpt_more = apply_filters('excerpt_more', ' ' . '[...]'); $words = preg_split("/[\n\r\t ]+/", $text, $excerpt_length + 1, PREG_SPLIT_NO_EMPTY); if ( count($words) > $excerpt_length ) { array_pop($words); $text = implode(' ', $words); $text = $text . $excerpt_more; } else { $text = implode(' ', $words); } } return apply_filters('wp_trim_excerpt', $text, $raw_excerpt); }
-
- 2016-09-15
使用上面的其他答案,这是一个似乎很有效的简单答案:
global $post; $excerpt = apply_filters('get_the_excerpt', get_post_field('post_excerpt', $post->ID)); if ( $excerpt == '' ) { $excerpt = wp_trim_words( $post->post_content, 55 ); }
我在函数的
<meta>
标记中使用它来定义OpenGraph描述.因此,我只需添加:<meta property="og:description" content="<?php echo esc_html( $excerpt ); ?>" />
Using others' answers above, here's a simpler answer that seems to work well:
global $post; $excerpt = apply_filters('get_the_excerpt', get_post_field('post_excerpt', $post->ID)); if ( $excerpt == '' ) { $excerpt = wp_trim_words( $post->post_content, 55 ); }
I'm using it in the
<meta>
tags in a function to define OpenGraph descriptions. So then I just add:<meta property="og:description" content="<?php echo esc_html( $excerpt ); ?>" />
-
那么HTML内容呢?标签将如何处理?摘录还剥离了html标签和简码.如果摘录的第一句话包含图片怎么办?那可能会破坏您的布局.What about HTML content? How will this deal with tags? the excerpt also strips html tags and shortcodes. what if the first words of the excerpt contain an image? That will probably break your layout.
- 0
- 2019-12-04
- brett
我正在建立一个主题,该主题将在主页上显示可能有数十篇帖子的节选.我没有所有文章的摘要,因此许多文章的
$post->post_excerpt
为空.如果没有手动摘录,我想使用内置的get_the_excerpt()函数,但在循环外部不可用.跟踪该函数,看起来它使用了wp-includes/formatting.php中的wp_trim_excerpt来动态创建摘录.我在
wp_trim_excerpt( $item->post_content )
这样的代码中调用它,但是它只是返回完整的内容.我在做错什么吗?我知道我可以创建自己的函数来创建摘录,但是我喜欢尽可能使用内置函数,以使我的代码与其他潜在的插件/过滤器兼容.
http://adambrown.info/p/wp_hooks/hook/wp_trim_excerpt?version=3.0&amp;file=wp-includes/formatting.php