WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Shortcodes don't work in excerpts? (9 posts)

  1. xsreality
    Member
    Posted 5 years ago #

    Hello,

    I am using the gallery shortcode CAPTION and it displays fine when a post is called through the_content() function. But the shortcode is displayed as it is when the post is called through the_excerpt() function. Any way to change that?

    Thanks,
    Abhinav

  2. moshu
    Member
    Posted 5 years ago #

    Not likely. Read about the_excerpt in the Codex: no code or html tags is executed in them.

  3. Michael Fields
    Themer
    Posted 5 years ago #

    xsreality,
    in your loop, you could try the following code:

    <?php
    print apply_filters( 'the_content', $post->the_excerpt );
    ?>

    Just a quick thought - not really tested, but I remember doing something similar to this in the past. I should also state that it is best practice to leave excerpts code free - like moshu suggested. Excerpts are used to build the data that gets inserted into you rss files and I have a seen a few plugins reek havoc on them.

    Good luck

  4. xsreality
    Member
    Posted 5 years ago #

    Thanx moshu and mfields... I have followed your advice and decided against using shortcodes at all. Somehow I find them unnecessarily cumbersome. There are other standard ways to achieve the same thing and I think I will stick to that! :)

  5. alexleonard
    Member
    Posted 5 years ago #

    It can be done!

    Add this to your functions.php file in your template. I've just tested it out and it works fine :)

    add_filter('the_excerpt', 'do_shortcode');

  6. adaptiman
    Member
    Posted 5 years ago #

    @alexleonard

    Which functions.php file? The one attached to the theme?

  7. timothyiv
    Member
    Posted 5 years ago #

    add_filter('the_excerpt', 'do_shortcode');

    in function.php Where to add it?

  8. Michael Fields
    Themer
    Posted 5 years ago #

    anywhere should work

  9. Drew Baker
    Member
    Posted 5 years ago #

    I'd like to remove the shortcodes from the_excerpt too. But I can't get it to work. Is my functions.php correct?

    <?php
    if ( function_exists('register_sidebar') )
    register_sidebar(array('name'=>'Main Sidebar',
            'before_widget' => '<li><div id="%1$s" class="%2$s">',
            'after_widget' => '</div></li>',
            'before_title' => '<h2 class="widgettitle">',
            'after_title' => '</h2>',
    		));
    register_sidebar(array('name'=>'Comments Sidebar',
            'before_widget' => '<li><div id="%1$s" class="%2$s">',
            'after_widget' => '</div></li>',
            'before_title' => '<h2 class="widgettitle">',
            'after_title' => '</h2>',
    		));
    register_sidebar(array('name'=>'Footer Sidebar',
            'before_widget' => '<li><div id="%1$s" class="%2$s">',
            'after_widget' => '</div></li>',
            'before_title' => '<h2 class="widgettitle">',
            'after_title' => '</h2>',
    		));
    add_filter('the_excerpt', 'do_shortcode');
    ?>

    Thanks for the help!

Topic Closed

This topic has been closed to new replies.

About this Topic