WordPress.org

Support

Support » How-To and Troubleshooting » Excerpt in a text widget on the actual post page?

Excerpt in a text widget on the actual post page?

  • 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?

Viewing 10 replies - 1 through 10 (of 10 total)
  • I think at least you need a dollar sign in front of post->ID:

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

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

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

    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); ?>

    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.

    IT WORKED! Thank you very VERY much, vtxyzzy!

    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.

    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?

    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.

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

Viewing 10 replies - 1 through 10 (of 10 total)
  • The topic ‘Excerpt in a text widget on the actual post page?’ is closed to new replies.