Site logo

[Resolved] Read More Button on Archives

Home Forums Support [Resolved] Read More Button on Archives

Home Forums Support Read More Button on Archives

Viewing 14 posts - 1 through 14 (of 14 total)
  • Author
    Posts
  • #1855906
    Michael

    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?

    #1855964
    Ying
    Staff
    Customer Support

    Hi Michael,

    Do you mean this read more button?
    https://www.screencast.com/t/Z3rssapg4ZNi

    Let me know 🙂

    #1855985
    Michael

    That is the one!

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

    Thanks

    #1855995
    Ying
    Staff
    Customer Support

    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 🙂

    #1856125
    Michael

    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.

    #1856169
    Elvin
    Staff
    Customer Support

    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.

    #1856174
    Michael

    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

    #1856207
    Elvin
    Staff
    Customer Support

    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? 😀

    #1857224
    Michael

    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.

    #1857634
    Elvin
    Staff
    Customer Support

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

    No problem. Glad you got it sorted. 😀

    #1859290
    Michael

    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?

    #1859315
    Ying
    Staff
    Customer Support

    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;
    }
    #1859323
    Michael

    Nailed it!

    Thank you so much for the quick and helpful response!

    #1859342
    Ying
    Staff
    Customer Support

    You are welcome 🙂

Viewing 14 posts - 1 through 14 (of 14 total)
  • You must be logged in to reply to this topic.