通过ID
-
-
内容的"某些"到底是什么?what exactly is "some" of the content?
- 0
- 2011-03-19
- kaiser
-
wordpress中的摘录功能返回帖子的摘录.如果帖子没有摘录,它将返回内容的一定数量的字符,后跟" ..."或"阅读更多"或模板提供的内容The excerpt function in wordpress returns the excerpt of a post. If the post does not have an excerpt it returns a certain number of characters of the content followed by '...' or 'read more' or whatever the template provides
- 0
- 2011-03-19
- Robin I Knight
-
不是成为PITA,而是社区规则禁止签名和标准关闭.为了遵守规则并避免在编辑所有问题后让[Jeff Atwood](http://stackexchange.com/about/management)向您发送严厉的消息,请停止使用*" Marvellous" *作为结尾.*(而且请不要射击信使)*Not to be a PITA but community rules disallow signatures and standard closings. So as to abide by the rules and avoid having [Jeff Atwood](http://stackexchange.com/about/management) send you a stern message after editing all your questions, please stop using *"Marvellous"* as a closing. *(And please don't shoot the messenger)*
- 1
- 2011-03-19
- MikeSchinkel
-
10 个回答
- 投票数
-
- 2011-03-19
您好 @Robin I.Knight:
我将
get_the_excerpt()
视为具有旧版设计的功能.随着WordPress使用量的增加,有很多新的用例不适合使用,但新的功能可以获取不同的数据.一个例子是现在经常使用$args
函数选项数组.但是很容易解决您的需求.您可以使用以下替代功能,将其放置在主题的
functions.php
文件中的任何位置:function robins_get_the_excerpt($post_id) { global $post; $save_post = $post; $post = get_post($post_id); $output = get_the_excerpt(); $post = $save_post; return $output; }
我尚未对其进行测试,但可以肯定的是我做对了.如果这不能满足您的需求,请详细说明,也许我可以提出其他建议.
Hi @Robin I. Knight:
I view
get_the_excerpt()
as a function with legacy design. As WordPress usage has grown there are many newer use-cases where it doesn't fit but where the newer functions for getting different data do. One example is the now frequent use of an$args
array of function options.But it's easy to fix for your needs. Here's an alternative function you can use which you can put anywhere in your theme's
functions.php
file:function robins_get_the_excerpt($post_id) { global $post; $save_post = $post; $post = get_post($post_id); $output = get_the_excerpt(); $post = $save_post; return $output; }
I've not tested it but am pretty sure I got it right. If this doesn't meet your needs please elaborate and maybe I can make other suggestions.
-
有用.有点.非常奇怪的结果.它肯定在执行其功能,但结果很奇怪.我将其与GET_Posts结合使用,由于某些原因,前2个get帖子始终相同.这是一个链接,您将明白我的意思.查看右侧的4个帖子.http://www.divethegap.com/update/community/feedback/2010/06/steve-riches/It works. Sort of. Very odd results. It is definitely performing its function but the results are odd. I am using it in conjunction with GET_Posts and for some reason the top 2 get posts are always the same. Here is a link and you will see what I mean. Look at the 4 posts on the right hand side. http://www.divethegap.com/update/community/feedback/2010/06/steve-riches/
- 0
- 2011-03-19
- Robin I Knight
-
@Robin I Knight:请发布您的循环源代码以更新您的问题;不看代码就很难调试代码.也可能是导致问题的插件;尝试一次禁用一个.@Robin I Knight: Please post your loop source code as an update to your question; it's very hard to debug code without seeing code. It's also possibly a plugin that is causing the problem; try disabling them one at a time.
- 0
- 2011-03-19
- MikeSchinkel
-
上面有问题的循环源代码^^Loop source code in question above ^^
- 0
- 2011-03-19
- Robin I Knight
-
顺便说一句,我将函数名称更改为get_the_excerpt_id($post_id)BTW I changed the name of the function to get_the_excerpt_id($post_id)
- 0
- 2011-03-20
- Robin I Knight
-
@Robin I Knight-我在循环中看不到任何东西,但是您可以尝试在循环开始时调用`setup_postdata($post)`,就像@Rarst所建议的那样.如果那不起作用,可能是您需要禁用的插件;你有尝试过吗?而且您可能不想将其称为"get_the_excerpt_id()",因为WordPress将来可能会添加该功能并破坏您的网站.通过使用`foreach($ stories as $ story),您可能会在循环中没有该函数:global $post;$post=$ story;`代替.@Robin I Knight - I don't see anything in your loop, but you might try calling `setup_postdata($post)` at the beginning of your loop, as @Rarst suggests. If that doesn't work it might be a plugin you need to disable; have you tried that? And you probably don't want to call it `get_the_excerpt_id()` because WordPress could add that function in the future and break your site. And you could probably do without the function in your loop by using `foreach($stories as $story): global $post; $post = $story;` instead.
- 0
- 2011-03-20
- MikeSchinkel
-
- 2011-03-20
摘录的技巧极其混乱.这不是您问题的精确答案,但是通常来说,如果您需要制作特定于Loop的模板标签,并使用
get_posts()
返回的数组,您可以像这样模拟Loop:$stories = get_posts(); foreach ($stories as $post) { setup_postdata($post); // stuff } wp_reset_postdata();
The mechanics of excerpt are extremely confusing. It is not precise answer to your question but in general if you need to make template tags, specific to Loop, work with array returned by
get_posts()
you can emulate Loop like this:$stories = get_posts(); foreach ($stories as $post) { setup_postdata($post); // stuff } wp_reset_postdata();
-
那么wp_reset_query()呢??what about wp_reset_query(); ?
- 0
- 2012-01-27
- cwd
-
@cwd,如果仅使用`setup_postdata()`全局查询不受影响,并且仅需要重置发布数据.@cwd if only using `setup_postdata()` global query is not affected and only post data needs to be reset.
- 1
- 2012-01-27
- Rarst
-
此解决方案比将桩存储在另一个var中并需要另一个桩才能使其全局更具清洁性.+1This solution is allot cleaner than storing the post in another var and requering another post just to get it global. +1
- 0
- 2013-04-10
- Barry Kooij
-
谢谢@Rarst帮助了我.添加setup_postdata($post);解决了我的问题Thanks @Rarst that helped me out. Adding setup_postdata($post); resolved my issues
- 0
- 2014-11-14
- Simon
-
- 2012-01-14
自3.3.0版以来,有一个新功能: wp_trim_words
我正在循环外使用它,如下所示:
<?php if ( $post_id ) { $post = get_post( $post_id ); if ( $post ) { ?> <h2><?php echo $post->post_title; ?></h2> <p><em><?php echo wp_trim_words( $post->post_content ); ?></em></p> <p><strong>This article can only be read by subscribers.</strong></p> <?php } } ?>
这不要与 wp_trim_excerpt 混淆,后者显然只在循环内起作用,因为它在内部调用the_content()
There is a new function since 3.3.0: wp_trim_words
I'm using it outside the loop as follows:
<?php if ( $post_id ) { $post = get_post( $post_id ); if ( $post ) { ?> <h2><?php echo $post->post_title; ?></h2> <p><em><?php echo wp_trim_words( $post->post_content ); ?></em></p> <p><strong>This article can only be read by subscribers.</strong></p> <?php } } ?>
This is not to be confused with wp_trim_excerpt that apparently only works within the loop, since it calls the_content() internally.
-
- 2013-08-30
仅添加到MikeSchinkel的答案中,由于某种原因,该答案对我不起作用.我必须添加setup_postdata行才能使其正常运行.
function get_the_excerpt( $post_id ){ global $post; $save_post = $post; $post = get_post($post_id); setup_postdata( $post ); // hello $output = get_the_excerpt(); $post = $save_post; return $output;
}
我假设如果您在循环之外使用它,那么它不应干扰正在进行的其他setup_postdata.
欢呼
Just to add to MikeSchinkel's answer, which for some reason wouldn't work for me. I had to add the setup_postdata line to make it work.
function get_the_excerpt( $post_id ){ global $post; $save_post = $post; $post = get_post($post_id); setup_postdata( $post ); // hello $output = get_the_excerpt(); $post = $save_post; return $output;
}
I'm assuming if you're using this outside the loop then it shouldn't interfere with other setup_postdata going on.
Cheers
-
我尝试了MikeSchinkel的答案,但它对我没有用.设置发布数据可以解决问题.在我没有'setup_postdata'的情况下,该函数返回了父帖子的title +摘录.I tried MikeSchinkel's answer and it did not work for me. Setting up post data did the trick. In my case without the 'setup_postdata' the function returned the title+excerpt of the parent post.
- 0
- 2016-09-25
- turzifer
-
- 2013-05-02
基于@Maxime的答案,这行得通吗?
$post = get_post( $id ); $excerpt = ( $post->post_excerpt ) ? $post->post_excerpt : $post->post_content;
对我来说似乎很直截了当,但是我想知道我是否缺少某些东西.
Building on @Maxime's answer, would this work?
$post = get_post( $id ); $excerpt = ( $post->post_excerpt ) ? $post->post_excerpt : $post->post_content;
It seems straight forward enough to me, but I'm wondering if I'm missing something.
-
- 2011-07-11
如果全部您的帖子带有
<!--more-->
标记,那么您可以在上面的代码中使用以下内容:$sjc_excerpt = explode( '<!--more-->', $post->post_content); echo wpautop( $sjc_excerpt[0] );
当然,如果您有任何没有
<!--more-->
标记的帖子,它们将完整显示.在我的情况下有效,但并非对所有人都适用...If ALL your posts have the
<!--more-->
tag, then you can use the following with your code above:$sjc_excerpt = explode( '<!--more-->', $post->post_content); echo wpautop( $sjc_excerpt[0] );
Of course if you have any posts that don't have the
<!--more-->
tag, they'll be shown in their entirety. Works in my situation, but not for all... -
- 2016-04-19
我将
get_the_excerpt()
视为具有旧版设计的功能.随着WordPress使用量的增加,有很多新的用例不适合使用,但新的功能可以获取不同的数据.一个例子是现在经常使用$args
函数选项数组.但是很容易解决您的需求.您可以使用以下替代功能,将其放置在主题的
functions.php
文件中的任何位置:function robins_get_the_excerpt($post_id) { global $post; $save_post = $post; $post = get_post($post_id); $output = get_the_excerpt(); $post = $save_post; return $output; }
仅添加到MikeSchinkel的答案中,由于某种原因,该答案对我不起作用.我必须添加setup_postdata行才能使其正常工作.
I view
get_the_excerpt()
as a function with legacy design. As WordPress usage has grown there are many newer use-cases where it doesn't fit but where the newer functions for getting different data do. One example is the now frequent use of an$args
array of function options.But it's easy to fix for your needs. Here's an alternative function you can use which you can put anywhere in your theme's
functions.php
file:function robins_get_the_excerpt($post_id) { global $post; $save_post = $post; $post = get_post($post_id); $output = get_the_excerpt(); $post = $save_post; return $output; }
Just to add to MikeSchinkel's answer, which for some reason wouldn't work for me. I had to add the setup_postdata line to make it work.
-
这需要`wp_reset_postdata()`在循环内工作吗?Does this need `wp_reset_post_data()` to work inside the loop?
- 0
- 2017-07-10
- Chris Pink
-
显然(绕过这栋特殊房子之后)现在已成为核心部分.Apparently (after going around this particular house) it's now part of core.
- 0
- 2017-07-10
- Chris Pink
-
- 2016-06-08
这是一个两层的代码,我经常使用 wp_trim_words 强>.我不断发现自己需要缩写,并且在循环之外阅读了更多功能.其他人可能会发现这很有用.这就是我用来:
- 通过POST ID获取摘要
- 获取帖子内容如果未设置摘录,
- 设置摘录的单词长度
- 选择内容以获取更多信息(链接/文本)
我将此行直接插入正在编辑的自定义模板中.
//Get Post Object $dapost = get_post(POST_ID); //Get the Execerpt $my_excerpt = wp_trim_words( apply_filters( "the_excerpt", get_the_excerpt($dapost) ? get_the_excerpt($dapost) : $dapost->post_content ), "20", "<a href='$dapost->guid'> ".__('Get More Stuff', 'translation')."</a>" );
崩溃
1.摘录内容
按帖子ID获取节选,但如果未设置节选,则获取帖子内容.
我正在使用If/Else PHP速记.
$dapost = get_post(POST_ID); apply_filters( "the_excerpt", get_the_excerpt($dapost) ? get_the_excerpt($dapost) : $dapost->post_content
2.字长
将摘录中的单词数设置为 20
"20"
3.选择ReadMore Content(链接/文本)
"<a href='$dapost->guid'> ".__('Get More Stuff', 'translation')."</a>"
我使用
$dapost->guid
来获取URL,因为我不需要友好的URL,并且想避免再次调用数据库.您可以始终使用get_the_permalink.请参阅Wordpress文档中的 wp_trim_words .
This is a little two-liner I use a lot utilizing wp_trim_words. I constantly finding myself needing the abbreviation and read more functionalities outside of the loop. Some one else may find this useful. So this is what I use to:
- Get the Excerpt by POST ID
- Get Post Content If no Excerpt has been set,
- Set the Word length of the Excerpt
- Choose the Content for the Read More(Link/Text)
I put this inline, directly in the custom template I am editing.
//Get Post Object $dapost = get_post(POST_ID); //Get the Execerpt $my_excerpt = wp_trim_words( apply_filters( "the_excerpt", get_the_excerpt($dapost) ? get_the_excerpt($dapost) : $dapost->post_content ), "20", "<a href='$dapost->guid'> ".__('Get More Stuff', 'translation')."</a>" );
Break Down
1.The excerpt content
Get the Excerpt by Post ID but, get Post Content If no Excerpt has been set.
I am using If/Else PHP shorthand.
$dapost = get_post(POST_ID); apply_filters( "the_excerpt", get_the_excerpt($dapost) ? get_the_excerpt($dapost) : $dapost->post_content
2. Word length
Set the amount of words in the Excerpt to 20
"20"
3. Choose ReadMore Content(Link/Text)
"<a href='$dapost->guid'> ".__('Get More Stuff', 'translation')."</a>"
I used
$dapost->guid
to get the URL, because I did not need friendly URLs, and wanted to avoid another call to the DB. You could always use get_the_permalink.See wp_trim_words in the Wordpress Documentation.
-
-
- 2018-09-07
可以从WP 4.5.0起使用帖子ID作为参数
get_the_excerpt( $post->ID )
来源: https://developer.wordpress.org/reference/functions/get_the_excerpt/
From WP 4.5.0 is possible use the post ID as parameter
get_the_excerpt( $post->ID )
Source:https://developer.wordpress.org/reference/functions/get_the_excerpt/
为什么不能像标题和大多数其他元素那样通过ID摘录.
例如get_the_excerpt(ID).我知道如何与$post->post_excerpt函数一起使用它,但是如果没有输入摘录,它不会返回部分内容,简单地什么也不会返回.
所以我想做的是,如果有摘录,则按ID摘录;如果没有该ID的摘录,但有内容,则取一些内容.
一个人会怎么做.
任何想法,
奇妙...
编辑-
按要求提供源代码.