Black Friday sale! Get 25% off WPSP Pro! Discount automatically applied at checkout.

[Resolved] Conditional 'Read More' text changes

Please login to receive premium support.

Support for the free plugin can be found here.

Home Forums Pro Support Conditional 'Read More' text changes

  • This topic has 11 replies, 2 voices, and was last updated 7 months ago by Tom.
Viewing 12 posts - 1 through 12 (of 12 total)
  • Author
    Posts
  • #14982
    Greg
    Participant

    Hi all

    Does anyone know of a way to add a function to change the ‘Read More’ text link based on a conditional such as a post tag? So for example for all posts tagged with ‘Photos’ the read more button text could be changed to ‘View Photos’, etc.

    I’ve found a couple of functions online but they seem to be theme specific.

    Thanks for any help
    Greg

    #14997
    Tom
    Keymaster

    Hi there,

    Not sure if it will work, but you can try this:

    add_filter( 'wpsp_read_more_output', function( $output ) {
        if ( ! has_tag( 'your-tag' ) ) {
            return $output;
        }
    
        return sprintf('<div class="wpsp-read-more"><a title="%1$s" class="%4$s" href="%2$s">%3$s</a></div>',
            the_title_attribute( 'echo=0' ),
            esc_url( get_permalink() ),
            'YOUR CUSTOM TEXT HERE',
            'wp-show-posts-read-more'
        );
    } );
    #15003
    Greg
    Participant

    Unfortunately not, Tom…

    “There has been a critical error on your website.
    Learn more about debugging in WordPress.”

    Any ideas?

    Thanks
    Greg

    #15037
    Tom
    Keymaster
    #15052
    Greg
    Participant

    Still the same unfortunately?

    #15057
    Tom
    Keymaster

    Can you check your error_log file for the exact error?

    #15064
    Greg
    Participant

    Hi Tom

    Error reads as follows…

    Fatal error: Uncaught ArgumentCountError: Too few arguments to function {closure}(), 1 passed in //wp-includes/class-wp-hook.php on line 287 and exactly 2 expected in /wp-content/plugins/code-snippets/php/snippet-ops.php(446) : eval()’d code:1 Stack trace: #0 /wp-includes/class-wp-hook.php(287): {closure}() #1 /wp-includes/plugin.php(206): WP_Hook->apply_filters() #2 /wp-content/plugins/wp-show-posts/inc/functions.php(240): apply_filters() #3 /wp-includes/class-wp-hook.php(287): wpsp_read_more() #4 /wp-includes/class-wp-hook.php(311): WP_Hook->apply_filters() #5 /wp-includes/plugin.php(478): WP_Hook->do_action() #6 /wp-content/plugins/wp-show-posts/wp-show-posts.php(477): do_action() #7 /wp-content/plugins/code-snippets/php/snippet-ops.php(446) : eval()’d code on line 1

    Thanks
    Greg

    • This reply was modified 7 months ago by Greg.
    #15096
    Tom
    Keymaster
    #15129
    Greg
    Participant

    Thanks Tom, that’s done it!

    If I wanted to create custom text for multiple tags and buttons, would you recommend me duplicating this snippet for each or is there a better way to combine into one snippet?

    Thanks for your perseverance
    Greg

    #15148
    Tom
    Keymaster

    You can use the same filter:

    add_filter( 'wpsp_read_more_output', function( $output ) {
        if ( has_tag( 'your-tag' ) ) {
            return sprintf('<div class="wpsp-read-more"><a title="%1$s" class="%4$s" href="%2$s">%3$s</a></div>',
                the_title_attribute( 'echo=0' ),
                esc_url( get_permalink() ),
                'YOUR CUSTOM TEXT HERE',
                'wp-show-posts-read-more'
            );
        }
    
        if ( has_tag( 'another-tag' ) ) {
            return sprintf('<div class="wpsp-read-more"><a title="%1$s" class="%4$s" href="%2$s">%3$s</a></div>',
                the_title_attribute( 'echo=0' ),
                esc_url( get_permalink() ),
                'YOUR CUSTOM TEXT HERE',
                'wp-show-posts-read-more'
            );
        }
    
        return $output;
    } );
    #15158
    Greg
    Participant

    Thanks Tom! Appreciate all the help with this, will mark as resolved 🙂

    #15179
    Tom
    Keymaster

    Glad I could help 🙂

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