Use the_excerpt without tags

Sooner or later, by using the_excerpt arise a number of challenges and questions. For example, in the some cases in the start of post should be used headers or any other tags (<strong>, <em>, etc) in this case, the_excerpt function, also output these tags in excerpt. It looks not good, moreover, quite often question arises as to remove not only tags, but also these tags content too.

WordPress the_excerpt without tags

Use the_excerpt two different the_excerpts for one post

Next task is not typical but more specific. Is it possible used two different the_excerpts for one post? The first, used for generation and outputting text in the tag <meta name="description" ...> that is this excerpt is intended more for search systems, than for users, although recent cans see it in the search results. The second excerpt, is intended for showing on a page as a post preview and more visitor oriented. Such an approach enough comfy, because provide create diverse content and more multilateral describe issue of article.

More one task. This time already from aesthetics area :) In the one of previous post we considered examples of used the_excerpt. Practical of each project there is a need to change quantity words of excerpt and end of excerpt. In terms of a clean code, it would be good implement all articulated by demands in one function, so that in future use it in each project. Below is solution such a task:

/* Show Excerpt Without Tags Or Shortcodes */
add_filter( 'get_the_excerpt', 'custom_get_the_excerpt_in_loop_helloadmin', 5);
function custom_get_the_excerpt_in_loop_helloadmin( $text ) {
  $raw_excerpt = $text;
  /*  You can change this condition. If you choose '' == $text, then will be excerpt for get_the_content(''). 
  Note: In this case "Excerpt" field on the "Edit Post" page must be empty, else function will be output contain 
  of the "Excerpt" field and in some cases will work incorrect. Used condition '' == !$text allow create 2 different excerpt. 
  First excerpt will used for output a content part (by default is 55 first words), without headers and their content, 
  also tags too. Second excerpt from "Excerpt" field on the "Edit Post" page, can use for output its contain in the 
  <decription> tag on the header. */
  if ( '' == !$text ) {
    // Retrieve the post content.
    $text = get_the_content(''); 
    //Remove shortcode tags from the given content.
    $text = strip_shortcodes( $text );
    $text = apply_filters('the_content', $text);
    $text = str_replace(']]>', ']]>', $text);
    // Regular expression that strips the header tags and their content.
    $regex = '#(<h([1-6])[^>]*>)\s?(.*)?\s?(<\/h\2>)#';
    $text = preg_replace($regex,'', $text);
    /* Change the excerpt word count. */
    $excerpt_word_count = 40;
    $excerpt_length = apply_filters('excerpt_length', $excerpt_word_count);
    /* Change the excerpt ending. */
		/* Uncomment it and remove string $excerpt_end = ' ...'; if you need replace default [...] to link of current post
		global $post;
		$excerpt_end = '<a href="'. get_permalink($post->ID) . '">Read more...</a>';
		*/
    $excerpt_end = ' ...';
    $excerpt_more = apply_filters('excerpt_more', ' ' . $excerpt_end);
    $excerpt = wp_trim_words( $text, $excerpt_length, $excerpt_more );
  }
  return apply_filters('wp_trim_excerpt', $excerpt, $raw_excerpt);
}

Leave Comment

Your email address will not be published. Required fields are marked *