• Hi, thanks for reading.

    I’m trying to execute a shortcode which exists in an excerpt on a site I’m working on.

    This works fine automatically using the_excerpt();

    But the_excerpt(); inserts an unnecessary <p>, which I do not want.

    So I’ve been using echo (get_the_excerpt();, which is fine except that it doesn’t execute the shortcode.

    I’ve played around with some filters and the do_shortcode($blah) stuff, all to zero effect.

    So if anybody could help me either:

    1. stop the_excerpt(); from inserting p tags, or
    2. make get_the_excerpt(); execute a shortcode

    I would be very much obliged.

    J

Viewing 14 replies - 1 through 14 (of 14 total)
  • Thread Starter Joshuwar

    (@joshuwar)

    Any ideas at all?

    Thread Starter Joshuwar

    (@joshuwar)

    Is this in the wrong place on the forum? Should I repost in another section?

    This is the right forum but I think that do_shortcode() should work and nobody has a better answer.

    Thread Starter Joshuwar

    (@joshuwar)

    OK. Well, for the record, here are things I’ve tried. Please let me know if anything doesn’t look right so I can try again.

    Replacing the string replace function in the get_the_excerpt function (I commented out the bit of trim_excerpt which gets rid of script etc):

    function improved_trim_excerpt($text) {
            global $post;
            if ( '' == $text ) {
                    $text = get_the_content('');
                    /*$text = apply_filters('the_content', $text);
                     $text = str_replace('\]\]\>', ']]>', $text);
                     $text = preg_replace('@<script[^>]*?>.*?</script>@si', '', $text);
                     $text = strip_tags($text, '<p>'); */
                    $excerpt_length = 80;
                    $words = explode(' ', $text, $excerpt_length + 1);
                    if (count($words)> $excerpt_length) {
                            array_pop($words);
                            array_push($words, '[...]');
                            $text = implode(' ', $words);
                    }
            }
            return $text;
    }
    
    remove_filter('get_the_excerpt', 'wp_trim_excerpt');
    add_filter('get_the_excerpt', 'improved_trim_excerpt');

    I also tried this, which I saw suggested in a few places:

    add_filter( 'get_the_excerpt', 'shortcode_unautop');
    add_filter( 'get_the_excerpt', 'do_shortcode');

    And I tried this, with and without an echo:

    do_shortcode(get_the_excerpt());

    None of these are working for me. I will either dig out the reason, or create a workaround which fulfils my requirement, and post it back here.

    I think you should try the do_shortcode with an echo:

    echo do_shortcode(get_the_excerpt());

    Thread Starter Joshuwar

    (@joshuwar)

    I tried that- it just echoes the unexecuted shortcode, like this:

    [shortcode] the text of the excerpt

    Will keep digging when I have a chance…

    Thread Starter Joshuwar

    (@joshuwar)

    PS. Thanks! (:

    ‘echo do_shortcode()’ should work. What code did you use to add the shortcode function?

    Thread Starter Joshuwar

    (@joshuwar)

    Sorry I don’t follow- on my page template I tried:

    do_shortcode(get_the_excerpt());

    and

    echo do_shortcode(get_the_excerpt());

    But neither execute the shortcode. Without the echo nothing is output at all, with the echo I get the text of the shortcode included in the excerpt, as described above.

    The shortcode I’m using is created by a plugin: Star-Rating-for-Reviews. This shortcode is executed perfectly when I call the_excerpt();, but not when using do_shortcode(get_the_excerpt);

    Am I missing something?

    The shortcode is probably registered by the plugin when you call the_excerpt and so is not registered otherwise.

    You should find the ‘add_shortcode()’ function call in the plugin and add it to your code.

    Thread Starter Joshuwar

    (@joshuwar)

    Interesting… So I searched for the add_shortcode() call in the plugin, and it wasn’t there. So I had a look for the mechanism it was using. It seems that it was doing a preg_replace on the shortcode, and dumping the results back into the content via a couple of filters:

    add_filter('the_content', 'sr_addstar');
    add_filter('the_excerpt', 'sr_addstar');

    I realised all I needed to do was add another line:

    add_filter('get_the_excerpt', 'sr_addstar');

    And hey- it worked.

    So it was the plugin. Thanks vtxyzzy for pointing me in the right direction.

    Any idea how to transform that code for wp-filebase and NextGen gallery?

    Moderator Jan Dembowski

    (@jdembowski)

    Forum Moderator and Brute Squad

    Fringer? You would be better served starting your own topic and providing more detail in that new topic.

    Well, I thought it would be better to keep it all in one topic, since I’m trying to achieve the same thing as the original poster, only with different plugins.

Viewing 14 replies - 1 through 14 (of 14 total)
  • The topic ‘executing shortcodes through get_the_excerpt();’ is closed to new replies.