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.

I want to remove the automatic paragraph tag that goes around my excerpts, which are being outputted by a custom function. I'd really appreciate if you could tell me what to add to my code.

I'm using this custom function for excerpts:

  // Default length (by WordPress)
  public static $length = 25;

  // So you can call: my_excerpt('short');
  public static $types = array(
      'short' => 5,
      'regular' => 25,
      'long' => 100
    );

  public static function length($new_length = 55) {
    Excerpt::$length = $new_length;
    add_filter('excerpt_length', 'Excerpt::new_length');
    Excerpt::output();
  }
  public static function new_length() {
    if( isset(Excerpt::$types[Excerpt::$length]) )
      return Excerpt::$types[Excerpt::$length];
    else
      return Excerpt::$length;
  }
  public static function output() {
    the_excerpt();
  }
}

// An alias to the class
function my_excerpt($length = 25) {
  Excerpt::length($length);
}

and calling it using

<?php my_excerpt('short'); ?>
share|improve this question

2 Answers 2

up vote 0 down vote accepted

If I'm understanding your question correctly, I think adding remove_filter( 'the_excerpt', 'wpautop' ); around when you have add_filter('excerpt_length', 'Excerpt::new_length'); does what you're looking for.

share|improve this answer

I know two good techniques that work fine with shortcodes and should work with excerpts as well.

You're able to parse excerpt's content using:

function my_excerpt( $content ) {

    /* Parse nested shortcodes and add formatting. */
    $content = trim( wpautop( do_shortcode( $content ) ) );

    /* Remove '</p>' from the start of the string. */
    if ( substr( $content, 0, 4 ) == '</p>' )
        $content = substr( $content, 4 );

    /* Remove '<p>' from the end of the string. */
    if ( substr( $content, -3, 3 ) == '<p>' )
        $content = substr( $content, 0, -3 );

    /* Remove any instances of '<p></p>'. */
    $content = str_replace( array( '<p></p>' ), '', $content );

    return $content;
}

In addition there is technique using [raw] shortcode:

function my_formatter($content) {
    $new_content = '';
    $pattern_full = '{(\[raw\].*?\[/raw\])}is';
    $pattern_contents = '{\[raw\](.*?)\[/raw\]}is';
    $pieces = preg_split($pattern_full, $content, -1, PREG_SPLIT_DELIM_CAPTURE);

    foreach ($pieces as $piece) {
        if (preg_match($pattern_contents, $piece, $matches)) {
            $new_content .= $matches[1];
        } else {
            $new_content .= wptexturize(wpautop($piece));
        }
    }

    return $new_content;
}

remove_filter('the_content', 'wpautop');
remove_filter('the_content', 'wptexturize');

add_filter('the_content', 'my_formatter', 99);

I hope it helps.

If you want us to help you with your function - it is always a good idea to say what is wrong with the function in the first place?

share|improve this answer
    
There's nothing wrong with the function. Wordpress automatically adds paragraph tags to excerpts, I want to remove the paragraph tags for this excerpt. –  AndrettiMilas Sep 1 '11 at 1:18

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.