6

I'm using the options framework

and i can't work out why this doesnt work

$x = of_get_option('post_number');
$content_post = get_post($x);
echo $content_post->post_excerpt;

its very odd because

echo of_get_option('post_number');

works perfectly and outputs a number

and according to get_post my code should work yet the echo produces nothing, not even an error message

so i must be handeling get_post() incorrectly somehow, any clues?


EDIT

var dump http://pastebin.com/ZEgQ5WPn reveals that post_content is full but post_excerpt is empty

how do i regenerate the excerpt?


EDIT [resolved]

i decided to manualy overwrite the excerpt but my option was missing, then i found this

and used

add_post_type_support( 'page', 'excerpt' );

to manualy write the excerpt

2
  • have you tried printing the object? ie: echo "<pre>"; print_r($content_post); echo "</pre>"; then see what thats outputting, chances are, post excerpt is empty?
    – Marty
    Jul 6, 2012 at 22:49
  • @Marty it outputs this - pastebin.com/ZEgQ5WPn
    – Neros
    Jul 6, 2012 at 22:57

3 Answers 3

30
$text = apply_filters('the_excerpt', get_post_field('post_excerpt', $post_id));
2
  • Seriously, this is awesome. Had no idea it existed.
    – jrobson153
    Mar 8, 2014 at 19:40
  • 1
    top quality an answer from 2014 still rules Apr 17, 2020 at 23:57
3

This will take the post_content and create an excerpt out of it. You can substitute the post_content for any other string of code. Change the 55 to another number to increase the amount of words returned.

$excerpt = wp_trim_words ( strip_shortcodes( $recent["post_content"], 55 ) );
2
  • this thread is 3 years old, i resolved the issue ages ago as indicated in the edits, ill make a self answer to close this
    – Neros
    Jan 29, 2015 at 0:15
  • You just like, saved mah loife. Thank you. May 8, 2015 at 3:55
1

You should be able to use get_post() like this, which returns almost all built-in post attributes as part of the post object.

<?php
  $my_id = 7;
  $my_post = get_post( $my_id ); 
  $my_excerpt = $my_post->post_excerpt;
  var_dump( $my_excerpt );
?> 

If that fails (it shouldn't, but perhaps you've tried by the sound of it) maybe checkout out WP_Query and pass in "p=$my_id" as a param. This is likely the function used under the get_post hood anyways.

<?php
  $my_id = 7;
  $my_posts = new WP_Query( "p=$my_id" ); 
  var_dump( $my_posts );
?> 
4
  • first code block returned : string(0) "" - second block did this pastebin.com/MDgRq2cq
    – Neros
    Jul 6, 2012 at 22:59
  • The blank string (versus null) suggests your excerpt is blank. maybe check out what this query returns (by hand): SELECT blueroom_posts.* FROM blueroom_posts WHERE 1=1 AND blueroom_posts.ID = 9 AND blueroom_posts.post_type = 'post' ORDER BY blueroom_posts.post_date DESC
    – buley
    Jul 6, 2012 at 23:00
  • but according to this other dump, its not empty pastebin.com/ZEgQ5WPn this thing is driving me nuts, everything looks like it should work, and there so little code to look through just makes it worse
    – Neros
    Jul 6, 2012 at 23:02
  • or at least it shows the content is full, but excerpt is empty, so wierd
    – Neros
    Jul 6, 2012 at 23:03

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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