• On my homepage template page-home.php I’m using this code to show 6 post previews including the excerpts for each post.

    <?php $posts = get_posts('orderby=date&numberposts=6'); foreach($posts as $post) { ?>
    			<section class="homeposts">
    			                          
    			
    			<a class="postfeatimg" style="background-image:url(<? if ( has_post_thumbnail() )
    						the_post_thumbnail_url('full'); ?>)"
    
    			href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>">	
    			</a>
    			<h2><?php the_title_attribute(); ?></h2>  
    			<?php if ( ! has_excerpt() ) {
          			echo get_the_excerpt();
    			} else { 
          		 the_excerpt();
    			} ?>
    				
    			</section>
    			<?php } ?> 

    I’ve added the code below to my functions.php

    function custom_excerpt_length( $length ) {
    			return 20;
    		}
    		add_filter( 'excerpt_length', 'custom_excerpt_length', 999 );

    Where I’m echoing get_the_excerpt(); it’s showing the page content excerpt rather than the excerpt of a post (I need to show post excerpts).

    After else the post excerpt isn’t getting trimmed as expected. Can anyone help to solve these two questions?

    Ideally I’d like post content to show where there’s no custom excerpt and for both to be trimmed with a ‘read more’ permalink. Thanks!

    • This topic was modified 7 years, 6 months ago by jeremybr0wn. Reason: formatted to be easier to read
Viewing 5 replies - 1 through 5 (of 5 total)
  • You need to use setup_postdata() so that template tags refer to the current post within a loop, otherwise it will refer to the main query, which will be for the page.

    <?php 
    $posts = get_posts('orderby=date&numberposts=6');
    foreach($posts as $post) : setup_postdata( $post ); ?>
    	<section class="homeposts">					  
    		// Link removed because WordPress forum doesn't like links in pre tags.
    		
    		<h2><?php the_title_attribute(); ?></h2>  
    
    		<?php the_excerpt(); ?>
    	</section>
    <?php endforeach; wp_reset_postdata(); ?> 
    

    Also, in my code I just replaced this code:

    <?php if ( ! has_excerpt() ) {
    	echo get_the_excerpt();
    } else { 
    	the_excerpt();
    } ?>
    

    with

    <?php the_excerpt(); ?>
    

    Because you’ll get the same result. I wasn’t sure what you were going for there.

    • This reply was modified 7 years, 6 months ago by Jacob Peattie.

    It looks to me that your if block is doing the opposite of what you actually want. You have:

    if ( ! has_excerpt() ) {
      echo get_the_excerpt();
    } else { 
      the_excerpt();
    }

    meaning if a post does not have a custom excerpt (NOT has_excerpt), you are asking to display custom excerpt. I think it should be other way round.

    if ( has_excerpt() ) {
      echo get_the_excerpt();
    } else { 
      the_excerpt();
    }

    What do you think?

    Thread Starter jeremybr0wn

    (@jeremybr0wn)

    Thanks both for the replies. I went with the modification from @jakept and with the second example from https://smallenvelop.com/limit-post-excerpt-length-in-wordpress/ I got the conclusion I’ve been looking for. Just one question!

    After using the following in my functions.php

    
    function get_excerpt(){
    $excerpt = get_the_content();
    $excerpt = preg_replace(" ([.*?])",'',$excerpt);
    $excerpt = strip_shortcodes($excerpt);
    $excerpt = strip_tags($excerpt);
    $excerpt = substr($excerpt, 0, 50);
    $excerpt = substr($excerpt, 0, strripos($excerpt, " "));
    $excerpt = trim(preg_replace( '/s+/', ' ', $excerpt));
    $excerpt = $excerpt.'... <a href="'.$permalink.'">more</a>';
    return $excerpt;
    }

    All the s characters disappeared from my excerpts. Removing the line $excerpt = trim(preg_replace( '/s+/', ' ', $excerpt)); seems to have fixed that problem but is there anything in this line that I need?

    Thanks

    Moderator bcworkz

    (@bcworkz)

    You need a backslash in the regexp to replace multiple whitespace chars with a single space: '/\s+/'

    Without the trim/preg_replace line you may get tabs, multiple spaces, and line feeds in your excerpt that look weird on output since they might occur in unintended places. Since they rarely occur in WP post content, you can probably do without the line, but the backslash will fix the problem if you wish to retain the functionality.

    Thread Starter jeremybr0wn

    (@jeremybr0wn)

    Thanks @bcworkz, that did it. For anyone else who searches this question, here’s what’s working for me:

    page-home.php

    <?php 
    	$posts = get_posts('orderby=date&numberposts=6');
    	foreach($posts as $post) : setup_postdata( $post ); ?>
    <section class="homeposts">
    	<h2><?php the_title_attribute(); ?></h2>  
    	<?php echo get_excerpt(); ?>
    </section>
    	<?php endforeach; wp_reset_postdata(); ?> 

    functions.php

    function get_excerpt(){
    $excerpt = get_the_content();
    $excerpt = preg_replace(" ([.*?])",'',$excerpt);
    $excerpt = strip_shortcodes($excerpt);
    $excerpt = strip_tags($excerpt);
    $excerpt = substr($excerpt, 0, 150);
    $excerpt = trim(preg_replace( '/\s+/', ' ', $excerpt));
    $excerpt = substr($excerpt, 0, strripos($excerpt, " "));
    $excerpt = $excerpt.'... <a href="'.get_the_permalink().'">more</a>';
    return $excerpt;
    }
    • This reply was modified 7 years, 6 months ago by jeremybr0wn.
Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Using Trim Except’ is closed to new replies.