• Hi,

    I am developing a custom theme for my portfolio. I’m trying to use the_excerpt to display a small description of the post in the thumbnail. I went to Settings > Reading > and switched Full Text for Summary

    This enabled my post page to display the excerpt section. Now, in my page-blog.php I have the following code:

    <?php
    
    get_header( 'blog');
    
    // we only retrieve the post for our blog
    $arg = array('category_name' => 'Blog');
    $category_posts = new WP_Query( $arg );
    
    ?>
    
    	  <?php if ( $category_posts->have_posts() ) :
    				while ( $category_posts->have_posts() ): ?>
    				<div class="blog-posts">
    				<?php $category_posts->the_post() ?>
    				<?php 	if (has_post_thumbnail() ):
    						the_post_thumbnail('thumbnail', array( 'class' => 'blog-thumb' ));
    						endif;
    				?>
    					<h2><a href="<?php the_permalink(); ?>"> <?php the_title()?> </a></h2>
    					<p> <?php the_time('F jS, Y' );?> </p>
    					<p class='entry'> <?php the_excerpt(); ?></p>
    				</div>
    				<?php
    				endwhile;
    			else: ?>
    				<p><?php _e( 'Sorry this page does not exist.' ); ?></p>
    	<?php 	endif;
    		wp_reset_postdata();
    			?>
    	<!-- we close container -->
    	</div>
    <?php
    // comments_template();
    get_footer();
    ?>

    I also removed in my functions.php the following filter, since I don’t want the excerpt to be populated with a default <p> tag:

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

    The problem that I’m having is that when the excerpt gets populated on the page, it doesn’t actually limit the word count to 55.

    Here is a link
    If the excerpt happens to have more than 55 words, they will be added regardless. I’m wondering if there’s something I have been doing wrong? I appreciate the help.

    Thanks!

Viewing 1 replies (of 1 total)
  • Thread Starter sheva29

    (@sheva29)

    Found a Solution but still not working. Reading from this post

    Apparently, when the excerpt doesn’t have any content, it runs wp_trim_excerpt() and truncates the text to 55 words. wp_trim_exceprt() only takes empty text as a parameter. So basically every time you have text in your Exceprt field in the CMS wp_trim_exceprt() doesn’t run. The person who posted in the forum added this snippet of code which I modified to show read more at the end like this:

    function rw_trim_excerpt( $text='' )
    {
        global $post;
        $text = strip_shortcodes( $text );
        $text = apply_filters('the_content', $text);
        $text = str_replace(']]>', ']]>', $text);
        $excerpt_length = apply_filters('excerpt_length', 55);
        $excerpt_more = apply_filters('excerpt_more', ' ' . '<a href="'. get_permalink($post->ID) . '">Read All ...</a>');
        return wp_trim_words( $text, $excerpt_length, $excerpt_more );
    }
    add_filter('wp_trim_excerpt', 'rw_trim_excerpt');

    This seems to work for me, now when I add this to limit the number of words in my excerpt it works:

    function wpdocs_custom_excerpt_length( $length ) {
        return 50;
    }
    add_filter( 'excerpt_length', 'wpdocs_custom_excerpt_length', 999 );

    I’m happy it works but I wonder if there’s something I’m still missing. I started this template from scratch, so there shouldn’t be any reason why I’m modifying the_exceprt() somewhere else.

    Once again, If anyone could elaborate on this I will really appreciate it.

    Thanks!

Viewing 1 replies (of 1 total)
  • The topic ‘the_excerpt() not working’ is closed to new replies.