Site logo

Archived topic

Readmore button duplicates when using WP show posts

5 replies · Started by mkjj on April 15, 2021

Viewing posts 1–6 of 6

Not quite sure whether this issue is related to GP or WP show posts. I use the code snippet described here to create readmore buttons for custom excerpts. I use WP show posts for the front page. With the snippet active the readmore button appears twice in the WP show posts section. I can fix this like so:

add_filter( 'wp_trim_excerpt', 'tu_excerpt_metabox_more' );
function tu_excerpt_metabox_more( $excerpt ) {
    $output = $excerpt;
	if ( !is_front_page() ) {
		if ( has_excerpt() ) {
			$output = sprintf( '%1$s <p class="read-more-button-container"><a class="read-more button" href="%2$s">%3$s</a></p>',
				$excerpt,
				get_permalink(),
				__( 'Weiterlesen →', 'generatepress' )
			);
		}
		
		return $output;
	}
}

Is there a better way to do this?

Hi there,

That looks good to me :)

It works ok in this case, but it would need more tweaking if I would use WP show posts on other pages. While this is not a big deal, I would prefer a less specific solution. It feels a little bit like a workaround. Just wanted to make sure that this is not a minor bug or conflict between GP and WP show posts. Is there a reason to not activate the readmore button for custom excerpts by default? I'm pretty sure most users find the missing button a little bit strange. It doesn't make sense to me that only the auto excerpt has a readmore link. Am I missing anything?

Hi there,

The simplest workaround for this is to disable WPSP's read more by leading the read more text blank.

wp_trim_excerpt filter applies site-wide. All loops that deal with $excerpt will show the button indicated on the code, whether it's from a plugin or a custom loop manually written.

The reason why the button duplicate is, the code adds a manual read more button. WPSP does this as well from this so this occurrence basically happens twice(one from your wp_trim_excerpt, another from add_action( 'wpsp_after_content','wpsp_read_more', 5 );), hence the duplicate button.

Thank you. Leaving the read more text blank will the alignment of the buttons not work anymore. My code snippet was not quite correct. The if-clause included the excerpt itself, what was not intended. This snippet now works:

add_filter( 'wp_trim_excerpt', 'tu_excerpt_metabox_more' );
function tu_excerpt_metabox_more( $excerpt ) {
	if ( is_front_page() ) {
		    $output = $excerpt;		
		return $output;
	}
	else {
		  $output = $excerpt;	
			  if ( has_excerpt() ) {
				  $output = sprintf( '%1$s <p class="read-more-button-container"><a class="read-more button" href="%2$s">%3$s</a></p>',
					  $excerpt,
					  get_permalink(),
					  __( 'Readmore', 'generatepress' )
				  );
			  }		
			  return $output;
	}
}

Leaving the read more text blank will the alignment of the buttons not work anymore.

Will need CSS for that.

The if-clause included the excerpt itself, what was not intended. This snippet now works:

Have you sorted the snippet's issue with WPSP as well?

Let us know.

This archived topic is closed to new replies.