0

I'm trying to use get_the_excerpt in edit-post to show show what a post will look like on the front-end, but it doesn't work. All other get_the_xxxx functions for the post works fine. I can show the title, date, metadata, but not the excerpt for some reason.

I tried this, didnt work either:

$text = apply_filters('the_excerpt', get_post_field('post_excerpt', $post_id));

Is there something preventing me from using get_the_excerpt on the edit-post page?

  • I can use get_the_excerpt on the front-end and it works fine. But on the edit-post page it returns nothing. If there is no custom excerpt created it should just get the first n words/characters from the post, which it does on the front-end. – kylmark Dec 2 '16 at 8:59
  • Yes but global $post is maybe not reachable in your function. You should try to add it if you don't (global $post; at the top of the function) – Benoti Dec 2 '16 at 9:45
0

I think (and assuming) you use (without code, it's hard to know, how you proceed) add_meta_boxes() to do your job. Then your code is logically standing in custom functions.

Your function to render the metabox, don't get the $post global, if you didn't define it, the function need to know from which post it must retrieve the excerpt, so you need to add global $post; this should work

function get_excerpt_custom_metabox(){
    global $post;

    echo get_the_excerpt();
}

The way you want to use the_excerpt filter is not the right way. You must do it like this

add_filter('the_excerpt', 'your_excerpt_function', 10, 2);

function your_excerpt_function($excerpt, $post_id){
    // do what you want,

    return $excerpt;
}

Don't hesitate to update your question with more code, if you think I'm wrong.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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