WordPress.org

Ready to get started?Download WordPress

Ideas

Get content/excerpt by id

  1. Ashworth Creative
    Member

    It's already pretty easy to get titles and permalinks by id, can there also be a simple function to get the content/excerpt of a post by id? This would make working with excerpts much easier, and make it easier to work with content outside the loop.

    Posted: 10 months ago #
  2. Ipstenu (Mika Epstein)
    Administrator

    Semi easy.

    If you use get_page() you can pull it:

    $page_data = get_page( $post_id );
    
        $excerpt = strip_tags($page_data->post_excerpt);
    
        echo $excerpt;
    Posted: 10 months ago #
  3. That doesn't apply the appropriate content filter to the excerpt, and it only works if there is a manual excerpt.

    Posted: 10 months ago #
  4. Ipstenu (Mika Epstein)
    Administrator

    You can use the same logic to pull in the first X characters of the content for an auto-excerpt, or better, make your own function to do both and call that instead.

    And what 'content filter' are you missing?

    Posted: 10 months ago #
  5. I'm referring to the the_excerpt and the_content filters. I was really hoping for native methods similar to get_the_title(), that pulls in the requested data and applies the relevant filter, based on a post id.

    Posted: 10 months ago #
  6. Ipstenu (Mika Epstein)
    Administrator

    Those functions were written around get_post() :)

    http://core.trac.wordpress.org/browser/tags/3.5.1/wp-includes/post-template.php#L260

    So my point was that you can do this, same way core did. Write your own function for it. The the_excerpt() stuff was explicitly written to work in the loop, which is why you'd have to make a second one for yourself. Adding in ways to make it work outside of the loop really would be the realm of a second filter, since it's part of the post template functions, which get content for the current post in the loop.

    It'd be a cool plugin to add in those functions, and if it got picked up a lot, it may end up in core :)

    Posted: 10 months ago #

RSS feed for this topic

Reply

You must log in to post.

  • Rating

    12345
    5 Votes
  • Status

    This idea is under consideration