WordPress.org

Ideas

Get content/excerpt by id

  1. Ashworth Creative
    Member

    12345

    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: 2 years ago #
  2. Ipstenu (Mika Epstein)
    Half-Elf; Volunteer Forum Mod & Plugin Referee

    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: 2 years ago #
  3. Ashworth Creative
    Member

    12345

    That doesn't apply the appropriate content filter to the excerpt, and it only works if there is a manual excerpt.

    Posted: 2 years ago #
  4. Ipstenu (Mika Epstein)
    Half-Elf; Volunteer Forum Mod & Plugin Referee

    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: 2 years ago #
  5. Ashworth Creative
    Member

    12345

    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: 2 years ago #
  6. Ipstenu (Mika Epstein)
    Half-Elf; Volunteer Forum Mod & Plugin Referee

    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: 2 years ago #
  7. Tekmoz
    Member

    I know this is an old topic now but I'm still having to use the preview button to get the id from the url, has there been any progress on this? If there has I must have missed it, is there an easier way to get the id yet?

    Posted: 1 year ago #

RSS feed for this topic

Reply

You must log in to post.

  • Rating

    12345
    6 Votes
  • Status

    This idea is under consideration