Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

wordpress get excerpt by id

Tags:

wordpress

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

like image 515
Neros Avatar asked Jul 06 '12 22:07

Neros


3 Answers

$text = apply_filters('the_excerpt', get_post_field('post_excerpt', $post_id));
like image 102
realmag777 Avatar answered Nov 02 '22 23:11

realmag777


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 ) );
like image 35
heathhettig Avatar answered Nov 02 '22 22:11

heathhettig


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 );
?> 
like image 29
buley Avatar answered Nov 03 '22 00:11

buley