Get post excerpt and title by specific post ID?

I’m trying to get post excerpt and title by specific post ID.
My post ID is 6 and my code as follows

<?php
    $id = 6;
    $post = get_post( $id );
    $excerpt = get_excerpt( $id);
    $excerpt = $post->post_excerpt;
?>
<h6><?php the_title(); ?></h6>

<?php echo get_excerpt(190); ?>

It shows the title of post ID 6, but the wrong excerpt…
Also I’ve an excerpt length control code in my functions.php

// Changing excerpt length
function get_excerpt($count){
    $permalink = get_permalink($post->ID);
    $excerpt = get_the_content();
    $excerpt = strip_tags($excerpt);
    $excerpt = substr($excerpt, 0, $count);
    $excerpt = substr($excerpt, 0, strripos($excerpt, " "));
    $excerpt = $excerpt.'... <a href="'.$permalink.'">Read More</a>';
    return $excerpt;
}

Any help will be appreciated…

5 Answers
5

Your get_excerpt() function uses the global $post variable, which is out of scope inside your function.

// Changing excerpt length
function get_excerpt($count){
    $permalink = get_permalink($post->ID);
    $excerpt = get_the_content();
    $excerpt = strip_tags($excerpt);
    $excerpt = substr($excerpt, 0, $count);
    $excerpt = substr($excerpt, 0, strripos($excerpt, " "));
    $excerpt = $excerpt.'... <a href="'.$permalink.'">Read More</a>';
    return $excerpt;
}

That means that when you do this:

$id = 6;
$post = get_post( $id );
$excerpt = get_excerpt( $id);

You are attempting to get the post content for the current post in the Loop truncated based on the ID of the post whose excerpt you are trying to get, but $post would be out of scope and thus “undefined”. Then, assuming $post were set,…

$excerpt = $post->post_excerpt;

… you are then completely overwriting that generated “excerpt” data with the raw excerpt data from the current post in the Loop.

And then…

echo get_excerpt(190);

… you grab the excerpt from the current post in the Loop again, and echo it.

What you are doing is quite wrong in several different ways. I have to assume that you’ve copied and pasted that code without understanding it, which is quite dangerous. I’d caution you against it.

The WordPress Core function get_the_title() will accept an ID argument, but get_the_excerpt() will not, so since you need both you are going to be best off simply retrieving the post object.

$id = 6;
$p = get_post($id);
// var_dump($p);
echo apply_filters('the_title',$p->post_title);
echo apply_filters('the_excerpt',$p->post_excerpt); // for a manually created excerpt

The generate an excerpt from post content, you’d need something like:

$text = $p->post_content;
$text = strip_shortcodes( $text );
$text = apply_filters( 'the_content', $text );
$text = str_replace(']]>', ']]&gt;', $text);
$excerpt_length = apply_filters( 'excerpt_length', 55 );
$excerpt_more = apply_filters( 'excerpt_more', ' ' . '[&hellip;]' );
$text = wp_trim_words( $text, $excerpt_length, $excerpt_more );
echo $text;

Which is really just a simplified version of the Core function wp_trim_excerpt()

You can control the length of the excerpt using the excerpt_length filter. Per the example in the Codex:

function custom_excerpt_length( $length ) {
    return 20;
}
add_filter( 'excerpt_length', 'custom_excerpt_length', 999 );

Leave a Comment