Home › Forums › Pro Support › Conditional 'Read More' text changes
- This topic has 11 replies, 2 voices, and was last updated 2 years, 9 months ago by
Tom.
-
AuthorPosts
-
April 23, 2020 at 3:47 am #14982
Greg
ParticipantHi 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
GregApril 23, 2020 at 5:13 pm #14997Tom
KeymasterHi 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' ); } );
April 24, 2020 at 4:09 am #15003Greg
ParticipantUnfortunately not, Tom…
“There has been a critical error on your website.
Learn more about debugging in WordPress.”Any ideas?
Thanks
GregApril 25, 2020 at 5:59 pm #15037Tom
KeymasterStrange, can you try the updated function?: https://wpshowposts.com/support/topic/conditional-read-more-text-changes/#post-14997
April 26, 2020 at 3:02 am #15052Greg
ParticipantStill the same unfortunately?
April 26, 2020 at 5:02 pm #15057Tom
KeymasterCan you check your
error_log
file for the exact error?April 27, 2020 at 12:56 am #15064Greg
ParticipantHi 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
GregApril 27, 2020 at 4:56 pm #15096Tom
KeymasterAha, can you try the updated code?: https://wpshowposts.com/support/topic/conditional-read-more-text-changes/#post-14997
April 28, 2020 at 2:04 am #15129Greg
ParticipantThanks 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
GregApril 28, 2020 at 4:25 pm #15148Tom
KeymasterYou 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; } );
April 29, 2020 at 4:31 am #15158Greg
ParticipantThanks Tom! Appreciate all the help with this, will mark as resolved 🙂
April 29, 2020 at 5:33 pm #15179Tom
KeymasterGlad I could help 🙂
-
AuthorPosts
- You must be logged in to reply to this topic.