Excerpt Displaying Problems
-
I have been using a custom excerpt; which works on all post except for 1. The problematic post’s content is just a small paragraph of texts. I have set a featured image for that post as well and it’s ID is 144. I have echoed it out so it can’t be wrong. The outcome is:
There’s a image but no excerpt!
Custom Excerpt:
function custom_wp_trim_excerpt($text) {
$raw_excerpt = $text;
if ( ” == $text ) {
//Retrieve the post content.
$text = get_the_content(”);//Delete all shortcode tags from the content.
$text = strip_shortcodes( $text );$text = apply_filters(‘the_content’, $text);
$text = str_replace(‘]]>’, ‘]]>’, $text);$allowed_tags = ‘<h3>,<h5>,<h4>,<p>’; /*** MODIFY THIS. Add the allowed HTML tags separated by a comma.***/
$text = strip_tags($text, $allowed_tags);global $post;
if($post->ID == 72 || 144){
//Find position of .
$find_pos = strpos($text, “.”);
//Get the header and first <p>
$text = substr($text, 0, $find_pos+1);
$excerpt_word_count = str_word_count($text);
}else{
$excerpt_word_count = 55;
}$excerpt_length = apply_filters(‘excerpt_length’, $excerpt_word_count);
$excerpt_end = ‘[…]’; /*** MODIFY THIS. change the excerpt ending to something else.***/
$excerpt_more = apply_filters(‘excerpt_more’, ‘ ‘ . $excerpt_end);$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 apply_filters(‘wp_trim_excerpt’, $text, $raw_excerpt);
}
remove_filter(‘get_the_excerpt’, ‘wp_trim_excerpt’);
add_filter(‘get_the_excerpt’, ‘custom_wp_trim_excerpt’);Usage:
<?php
global $wp_query;
$args = array_merge( $wp_query->query, array( ‘post_type’ => ‘post’, ‘post__in’ => array(144) ) );
query_posts( $args );?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<h3>our products</h3>
<div class=”img-left”><?php the_post_thumbnail(); ?></div>
<?php the_excerpt();?>
<?php endwhile; wp_reset_query(); else: ?>
<p><?php _e(‘Sorry, no posts matched your criteria.’); ?></p>
<?php endif; ?>Anybody can spot any problems please reply asap. Thx.
- The topic ‘Excerpt Displaying Problems’ is closed to new replies.