WordPress.org

Ready to get started?Download WordPress

Forums

Excerpt in a text widget on the actual post page? (11 posts)

  1. drokkon
    Member
    Posted 2 years ago #

    I'm using the excerpt field on my blog in addition to the "more" tag. The front page shows each post up to the "more" tag with a "read more" button, but I use the excerpt field to store the author's synopsis of the article. The only place I want to display the excerpt is on the actual post page, in a text widget. Naively, I thought I could just call the_excerpt() or get_the_excerpt() in the text widget.

    Boy was I wrong.

    After a crash course in the loop, I now understand why that wouldn't work. I've found a lot of tutorials showing how to add a function and then call that function from the widget in order to get things that are "inside" the loop such as the excerpt. Unfortunately, I need to pass the post ID to the function, and my text widget doesn't seem to know how to deal with the post->ID. In other words, if I put in

    <?php get_excerpt_outside(post->ID); ?>

    the widget will output

    ID); ?>

    (And that's assuming I've even written the function correctly.)

    I know I'm clueless and don't have any business attempting this on my own, but I can't find a simple plugin to accomplish it. Can anyone offer any advice?

  2. vtxyzzy
    Member
    Posted 2 years ago #

    I think at least you need a dollar sign in front of post->ID:

    <?php get_excerpt_outside($post->ID); ?>

  3. drokkon
    Member
    Posted 2 years ago #

    Thanks, vtxyzzy. Sorry I mistyped that. There is a dollar sign there, and my text widget still doesn't seem to parse it correctly.

  4. vtxyzzy
    Member
    Posted 2 years ago #

    Please put the code for the function in a pastebin and post a link to it here.

  5. drokkon
    Member
    Posted 2 years ago #

    I've tried about three different functions I found online...I can't really test any of them because I can't pass them a variable without the text widget parsing my call incorrectly.

    Here's an example:

    function get_the_excerpt_here($post_id)
    {
      global $wpdb;
      $query = "SELECT post_excerpt FROM $wpdb->posts WHERE ID = $post_id LIMIT 1";
      $result = $wpdb->get_results($query, ARRAY_A);
      return $result[0]['post_excerpt'];
    }

    Presumably called with something akin to:

    <?php get_the_excerpt_here($post->ID); ?>

  6. vtxyzzy
    Member
    Posted 2 years ago #

    Sorry for the brain fog. I just realized that you are trying to use PHP inside the text widget.

    Try using the My Custom Widget plugin that will let you put PHP inside its widgets.

  7. drokkon
    Member
    Posted 2 years ago #

    IT WORKED! Thank you very VERY much, vtxyzzy!

  8. drokkon
    Member
    Posted 2 years ago #

    BTW...I didn't need to add a function. It was as simple as adding this to the widget:

    $your_post = get_post($post->ID);
    $your_excerpt = $your_post->post_excerpt;
    echo $your_excerpt;

    Hope that helps someone else.

  9. drokkon
    Member
    Posted 2 years ago #

    FYI...that particular plugin broke users on my network (see http://wordpress.org/support/topic/user-disappeared-from-site-cannot-add-back).

    Any other suggestions for a PHP widget plugin?

  10. drokkon
    Member
    Posted 2 years ago #

    Trying Samsarin PHP Widgets...works okay, but it doesn't allow for other custom widget plugins. In other words, I'm using RocketTheme's gantry plugin which gives me a few dropdowns to "style" my widgets using their theme; unfortunately, these choices don't show up within Samsarin's widget so it can't be styled.

  11. drokkon
    Member
    Posted 2 years ago #

    NM...PHP Code Widget, aptly named, did the trick.

Topic Closed

This topic has been closed to new replies.

About this Topic