Reputation: 527
I am trying to get this wordpress page template to display an excerpt from a specific post. When the post was created I was sure to insert the link in the place that I want. I am able to grab the title, the thumbnail, the permalink, etc... but for whatever reason I can not get the excerpt. I have tried:
the_excerpt();
get_the_excerpt();
the_content('',FALSE);
get_the_content('', FALSE, '');
get_the_content('', TRUE);
Among other things. When I try get_the_content('', TRUE)
it gives me the content from everything AFTER the link but I want what is BEFORE the link.
Any ideas?
<?php
$query = 'cat=23&posts_per_page=1';
$queryObject = new WP_Query($query);
?>
<?php if($queryObject->have_posts()) : ?>
<div>
<?php while($queryObject->have_posts()) : $queryObject->the_post() ?>
<div>
<h1><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h1>
<br>
<?php the_post_thumbnail() ?>
<?php #the_excerpt(); ?>
<div>
<a href="<?php the_permalink(); ?>">Read More</a>
</div>
</div>
<?php endwhile ?>
</div>
<?php endif; wp_reset_query();
?>
Upvotes: 2
Views: 24449
Reputation: 1600
Here's a pretty neat solution that'll do the trick for you!
<div class="post">
<h3 class="title"><?php echo $post->post_title ?></h3>
<?
// Making an excerpt of the blog post content
$excerpt = strip_tags($post->post_content);
if (strlen($excerpt) > 100) {
$excerpt = substr($excerpt, 0, 100);
$excerpt = substr($excerpt, 0, strrpos($excerpt, ' '));
$excerpt .= '...';
}
?>
<p class="excerpt"><?php echo $excerpt ?></p>
<a class="more-link" href="<?php echo get_post_permalink($post->ID); ?>">Read more</a>
</div>
Upvotes: 5
Reputation: 527
Ok here is what I came up with. Probably better solutions but it works!
function get_excerpt(){
$page_object = get_page( $post->ID );
$content = explode('<!--more-->', $page_object->post_content);
return $content[0];
}
then call it like this:
<?php echo get_excerpt(); ?>
Upvotes: 3
Reputation: 6052
Try adding this to your functions.php and calling the excerpt by post ID:
//get excerpt by id
function get_excerpt_by_id($post_id){
$the_post = get_post($post_id); //Gets post ID
$the_excerpt = ($the_post ? $the_post->post_content : null); //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;
return $the_excerpt;
}
Then call it in your template like this:
get_excerpt_by_id($post->ID);
Upvotes: 5