admin管理员组文章数量:1319007
Why is it that one cannot get the excerpt by ID like with the title and most other elements.
eg. get_the_excerpt(ID). I know how to use it with the $post->post_excerpt function but that does not return part of the content if no excerpt was entered it simple returns nothing.
So what I am trying to do is get the excerpt by ID if there is an excerpt, and if there isn't an excerpt with that ID but there is some content, to get some of the content instead.
How would one do that.
Any ideas,
Marvellous ...
EDIT --
Loop Source Code as Requested.
<?php $stories = get_posts('category_name=feedback&numberposts=4');
foreach ($stories as $post) :
$title = $post->post_title;
$excerpt = get_the_excerpt_id($post->ID);
$thumb = get_the_post_thumbnail($post->ID,array(60, 60, true));?>
<div class="textstandard_white" style="font-size:14px; line-height:22px; padding-top:10px;"><b><a href="<?php echo get_permalink($post->ID);?>"><?php echo $title;?></a></b></div><div align="left" style="height:18px; width:82px; background:url(.png) left top no-repeat;"><div id="stars<?php echo $post->ID;?>" align="left" style="height:18px; background:url(.png) left top no-repeat;">
</div>
</div>
<script type="text/javascript">
var width<?php echo $post->ID;?> = ((<?php
$Rating = get_post_meta($post->ID, "Rating", true);
echo $Rating;
?> * 20) + '%')
$('#stars<?php echo $post->ID;?>').css('width', width<?php echo $post->ID;?>);
</script><div class="textstandard_white" style="padding-top:6px; font-size:10px; color:#BBB; padding-bottom:10px; border-bottom:1px dotted #BBB; min-height:70px;"><div style="float:left; padding-right:6px; padding-bottom:6px;"><div style="background:#FFF; border:1px solid #FFF;
border-radius: 4px; -moz-border-radius: 4px ; -webkit-border-radius: 4px; padding:4px;"><a href="<?php echo get_permalink($post->ID);?>"><?php echo $thumb;?></a></div></div>
<?php echo $excerpt;?></div>
<?php endforeach;?>
Why is it that one cannot get the excerpt by ID like with the title and most other elements.
eg. get_the_excerpt(ID). I know how to use it with the $post->post_excerpt function but that does not return part of the content if no excerpt was entered it simple returns nothing.
So what I am trying to do is get the excerpt by ID if there is an excerpt, and if there isn't an excerpt with that ID but there is some content, to get some of the content instead.
How would one do that.
Any ideas,
Marvellous ...
EDIT --
Loop Source Code as Requested.
<?php $stories = get_posts('category_name=feedback&numberposts=4');
foreach ($stories as $post) :
$title = $post->post_title;
$excerpt = get_the_excerpt_id($post->ID);
$thumb = get_the_post_thumbnail($post->ID,array(60, 60, true));?>
<div class="textstandard_white" style="font-size:14px; line-height:22px; padding-top:10px;"><b><a href="<?php echo get_permalink($post->ID);?>"><?php echo $title;?></a></b></div><div align="left" style="height:18px; width:82px; background:url(http://www.divethegap/update/z-images/structure/icons/stars.png) left top no-repeat;"><div id="stars<?php echo $post->ID;?>" align="left" style="height:18px; background:url(http://www.divethegap/update/z-images/structure/icons/stars_glow.png) left top no-repeat;">
</div>
</div>
<script type="text/javascript">
var width<?php echo $post->ID;?> = ((<?php
$Rating = get_post_meta($post->ID, "Rating", true);
echo $Rating;
?> * 20) + '%')
$('#stars<?php echo $post->ID;?>').css('width', width<?php echo $post->ID;?>);
</script><div class="textstandard_white" style="padding-top:6px; font-size:10px; color:#BBB; padding-bottom:10px; border-bottom:1px dotted #BBB; min-height:70px;"><div style="float:left; padding-right:6px; padding-bottom:6px;"><div style="background:#FFF; border:1px solid #FFF;
border-radius: 4px; -moz-border-radius: 4px ; -webkit-border-radius: 4px; padding:4px;"><a href="<?php echo get_permalink($post->ID);?>"><?php echo $thumb;?></a></div></div>
<?php echo $excerpt;?></div>
<?php endforeach;?>
Share
Improve this question
edited Mar 19, 2011 at 20:27
Robin I Knight
asked Mar 19, 2011 at 17:34
Robin I KnightRobin I Knight
1,5617 gold badges21 silver badges28 bronze badges
3
- what exactly is "some" of the content? – kaiser Commented Mar 19, 2011 at 17:47
- 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 – Robin I Knight Commented Mar 19, 2011 at 17:48
- 1 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 send you a stern message after editing all your questions, please stop using "Marvellous" as a closing. (And please don't shoot the messenger) – MikeSchinkel Commented Mar 19, 2011 at 18:26
11 Answers
Reset to default 21Hi @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.
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();
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.
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
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.
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...
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.
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.
This worked for me:
$excerpt = apply_filters('the_excerpt', get_post_field('post_excerpt', $post_ide));
From WP 4.5.0 is possible use the post ID as parameter
get_the_excerpt( $post->ID )
Source:https://developer.wordpress/reference/functions/get_the_excerpt/
As annoying as it can be, it's actually fairly straightforward to do
function cameronjonesweb_get_excerpt_by_id( $post_id ) {
return apply_filters( 'get_the_excerpt', wp_trim_excerpt( '', $post_id ), $post_id );
}
本文标签: postsGET the excerpt by ID
版权声明:本文标题:posts - GET the excerpt by ID 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742053089a2418154.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论