Using Trim Except
-
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!
Viewing 5 replies - 1 through 5 (of 5 total)
Viewing 5 replies - 1 through 5 (of 5 total)
- The topic ‘Using Trim Except’ is closed to new replies.