Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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?

share|improve this question
add comment

2 Answers

up vote 1 down vote accepted

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.

share|improve this answer
    
Thanks a lot. Great, thorough answer. Definitely helps. –  BigToe Feb 16 '11 at 16:24
add comment

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.

share|improve this answer
    
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
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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