1

Using the Wordpress default theme 2010 1.1, shortcodes work on the main blog page and individual post pages, but are being stripped out on category and archive pages.

For example, stick in the theme's functions.php:

add_shortcode('tsc', 'tsc_process_shortcode' );

function tsc_process_shortcode($atts, $content = null) {
return 'INSERTED TEXT ' . $content;
}

INSERTED TEXT isn't generated from [tsc] in post content shown on category and archive pages. How do I get shortcodes to function on category and archive pages?

1

Shortcodes are stripped from the excerpt before filters are applied. Try something more like this:

function tsc_execute_shortcodes_in_excerpts( $text, $raw_text ){
  if(empty($raw_text)){
    $text = get_the_content('');
    $text = apply_filters('the_content', $text);
    $text = str_replace(']]>', ']]>', $text);
    $text = strip_tags($text);
    $excerpt_length = apply_filters('excerpt_length', 55);
    $excerpt_more = apply_filters('excerpt_more', ' ' . '[...]');
    $words = preg_split("/[\n\r\t ]+/", $text, $excerpt_length + 1, PREG_SPLIT_NO_EMPTY);
    if ( count($words) > $excerpt_length ) {
      array_pop($words);
      $text = implode(' ', $words);
      $text = $text . $excerpt_more;
    } else {
      $text = implode(' ', $words);
    }
  }
  return $text;
}

add_filter( 'wp_trim_excerpt', 'tsc_execute_shortcodes_in_excerpts', 10, 2 );

That basically re-runs wp_trim_excerpt() on the content (minus shortcode stripping) if it was trimmed to begin with. If you want to target your shortcode only, you could do something like this after get_the_content(''):

$text = str_replace( '[tsc', '{tsc', $text );
$text = strip_shortcodes( $text );
$text = str_replace( '{tsc', '[tsc', $text );

Hope that helps.

  • Thanks a lot. Great, thorough answer. Definitely helps. – BigToe Feb 16 '11 at 16:24
1

Assuming that you are using the default 2010 the archive and category loops use the_excerpt instead of the_content and that is why the shortcodes are not being generated, fortunately there is a simple fix for that , just add this line to your themes functions.php file

add_filter( 'the_excerpt', 'do_shortcode', 11 );

Hope this helps.

  • Spot on, +1.. :) Alternative would be to just switch the_excerpt calls with the_content calls.. – t31os Feb 15 '11 at 23:08
  • Thanks for the fast reply. I had already tried that. Doesn't work. I'm running WP 3.0.5. I can't understand what's going on. – BigToe Feb 15 '11 at 23:14
  • By the way, I'm not doing anything fancy. Vanilla, clean installation of Wordpress on XAMPP. – BigToe Feb 15 '11 at 23:15

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.