Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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...

share|improve this question

5 Answers 5

<?php echo get_excerpt(190); ?> this part is wrong. You are printing excerpt from id = 190 just use echo $excerpt. Also you don't need the line <?php echo get_excerpt(190); ?>

Read the codex pages carefully you will see lots of helpful examples there to understand how a function works http://codex.wordpress.org/Function_Reference/get_post

share|improve this answer
    
not working... <?php echo get_excerpt(190); ?> control the excerpt... –  Gopal Bhattacharjee Dec 18 '12 at 7:56
    
also i'hv a expert length in function.php –  Gopal Bhattacharjee Dec 18 '12 at 8:40

It looks like you've got a lot happening...

I'd try the following:

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

And use the following to display the excerpt:

echo $excerpt;
share|improve this answer

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 );
share|improve this answer

Why don't you echo $excerpt variable? I think this should work. Don't do this:

<?php echo get_excerpt(190); ?>

do this:

<?php echo $excerpt; ?>
share|improve this answer
    
excerpt showing but no read more link... –  Gopal Bhattacharjee Dec 18 '12 at 12:11
    
the problem is resolved...thanks –  Gopal Bhattacharjee Dec 19 '12 at 2:34

If you want to get the expert of a specific post with id 6, you can simply do as follow:

<?php
$id = 6;
$posts = get_posts($id)
foreach($posts as $post):?>
<h6><?php echo $post->post_title;?></h6>
<p><?php echo $post->post_excerpt();?></p>
<?php endforeach; ?>

This will simply give the title and the excerpt of the post with id 6.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.