Site logo

Archived topic

Read More Button on Archives

13 replies · Started by Michael on July 14, 2021

Viewing posts 1–14 of 14

I'm using manual excerpts and the Read more button was not appearing in the archives.

I added the Code Snippets plugin and used the Read More PHP from this article: https://docs.generatepress.com/article/activating-read-custom-excerpt/

Unfortunately, the Read more block now shows up on all pages with custom excerpts, not just the archives. This page demonstrates the issue: https://staging-studentloansherpa.kinsta.cloud/public-service-student-loan-forgiveness-basics-fine-print/

Is there a way to address this issue?

That is the one!

If we could get rid of that, I’d be thrilled.

Thanks

Try this PHP snippet instead:

add_action( 'generate_after_entry_content', 'tu_static_read_more_button' );
function tu_static_read_more_button() {
?>
    <?php if ( is_home() || is_archive() ) : ?>
        <p class="read-more-container">
            <a href="<?php the_permalink(); ?>" class="button">Read more</a>
        </p>
    <?php endif; ?>
<?php
}

Let me know :)

Good news and bad news.

The original problem is fixed, but a new one was created.

The read more block looks good for posts with a custom excerpt, and the posts look correct. However, the posts on archive pages that do not have a custom excerpt now have two read more buttons.

Example: https://staging-studentloansherpa.kinsta.cloud/category/repayment/

Also, the read more button now comes immediately after the except text. I'd like to add a bit of space if possible.

Thanks again for the help.

Hi Michael,

If you must apply the code coming from this documentation - https://docs.generatepress.com/article/activating-read-custom-excerpt/ - only on archive pages, you can add a condition for it.

Say for example:

add_filter( 'wp_trim_excerpt', 'tu_excerpt_metabox_more' );
function tu_excerpt_metabox_more( $excerpt ) {
    $output = $excerpt;

    if ( has_excerpt() ) {
        if(is_archive() || is_home()) {
            $output = sprintf( '%1$s <p class="read-more-button-container"><a class="button" href="%2$s">%3$s</a></p>',
                $excerpt,
                get_permalink(),
                __( 'Read more', 'generatepress' )
            );
        }
    }
	
    return $output;
}

You can try replacing Ying's code with this.

that code did not work for me.

I got this warning:

Don't Panic

The code snippet you are trying to save produced a fatal error on line 15:

Unclosed '{' on line 2

Can you screenshot the code added?

That error means there's a missing }.

Perhaps the last } on the bottom of the code (under return $output;) wasn't copied? :D

That is quite embarrassing.

Your code worked perfectly... the only flaw was my ability to copy and paste.

Thank you for the help. I got started with generatepress a couple of weeks ago and could not be more thrilled with everything. The site is incredibly fast, looks great, and has been easy to set up. I really appreciate it.

That happens to me from time to time as well. lol.

No problem. Glad you got it sorted. :D

I think I need to revive this discussion.

I noticed that the read more buttons are also missing in search results. Is there a way to fix this?

Hi Micheal,

We can add search result page into the PHP snippet as well, try this:

add_filter( 'wp_trim_excerpt', 'tu_excerpt_metabox_more' );
function tu_excerpt_metabox_more( $excerpt ) {
    $output = $excerpt;

    if ( has_excerpt() ) {
        if(is_archive() || is_home() || is_search() ) {
            $output = sprintf( '%1$s <p class="read-more-button-container"><a class="button" href="%2$s">%3$s</a></p>',
                $excerpt,
                get_permalink(),
                __( 'Read more', 'generatepress' )
            );
        }
    }
	
    return $output;
}

Nailed it!

Thank you so much for the quick and helpful response!

You are welcome :)

This archived topic is closed to new replies.