0

I have a shortcode, [foo], which displays the value in a custom field. However, as it appears near the top of each post, it is included in the excerpt.

function foo_shortcode(){
    global $post;
    $foo = get_post_meta( $post->ID, 'foo', true );
    return $foo;
}
add_shortcode( 'foo', 'foo_shortcode' );

The shortcode works successfully on the posts single, but not on the archive.

What I am missing?

  • Further research implies that Wordpress strips shortcodes out of excerpts and they can be enabled with add_filter('the_excerpt', 'do_shortcode');. However, this doesn't seem to work. – mistertaylor Mar 30 '15 at 15:37
  • What is the code responsible for excerpt output in template? – Rarst Mar 30 '15 at 15:54
1

The do_shortcode function runs on the the_content hook:

402   
403   add_filter('the_content', 'do_shortcode', 11); // AFTER wpautop()

https://core.trac.wordpress.org/browser/tags/4.1.1/src/wp-includes/shortcodes.php#L403

So, without seeing your theme code I am going to guess that your theme's archive page is written in such a way that the do_shortcode function does not fire-- possibly because it uses the_excerpt instead of the_content. If that is the case, you can force the processing of shortcodes with:

add_filter('the_excerpt', 'do_shortcode');

However, you will need to watch that as some shortcodes may generate content that you don't want in the excerpts.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.