Support » Plugin: Related Posts by Taxonomy » Can I set a limit on the number of characters that appear?

Viewing 7 replies - 1 through 7 (of 7 total)
  • Plugin Author keesiemeijer

    (@keesiemeijer)

    Hi saadfaris

    For post titles or image captions?
    Do you need this for the widget or the shortcode, or both?

    Just For post titles. and I need this for the shortcode.

    Thank you for the quick answer ^^ز

    Plugin Author keesiemeijer

    (@keesiemeijer)

    Ah, ok.

    I will give you an example on how you could do this tomorrow. It’s getting a bit too late for me right now.

    ok,

    I am waiting for you.

    Plugin Author keesiemeijer

    (@keesiemeijer)

    I’ve come up with two versions. One where you can limit by words, and one where you can limit by characters.

    This example limits the title by three words. Put this in your (child) theme’s functions.php file.

    
    add_filter( 'related_posts_by_taxonomy_post_link', 'rpbt_limit_post_titles_by_words', 10, 3 );
    
    function rpbt_limit_post_titles_by_words(  $link, $post, $args ) {
    	$words = 3;
    
    	if ( $args['title'] && $args['permalink'] ) {
    		$more  = __( '…' );
    		$title = wp_trim_words( $args['title'], $words, $more );
    		$link  = "<a href='{$args['permalink']}'>{$title}</a>";
    	}
    
    	return $link;
    }

    The next example limits the title by 12 characters. Put it in your (child) theme’s functions.php file.

    
    add_filter( 'related_posts_by_taxonomy_post_link', 'rpbt_limit_post_titles_by_characters', 10, 3 );
    
    function rpbt_limit_post_titles_by_characters(  $link, $post, $args ) {
    	$characters = 12;
    
    	if ( $args['title'] && $args['permalink'] ) {
    		$title = wp_strip_all_tags( trim( $args['title'] ) );
    		if ( strlen( $title ) > $characters ) {
    			$more  = __( '&hellip;' );
    			$title = substr( $title, 0, $characters ) . $more;
    		}
    
    		$link = "<a href='{$args['permalink']}'>{$title}</a>";
    	}
    
    	return $link;
    }
    

    btw:
    consider creating a child theme instead of editing your theme directly – if you upgrade the theme all your modifications will be lost. Or create a plugin with the code above.

    This is awesome thanks for helping me

    Plugin Author keesiemeijer

    (@keesiemeijer)

    You’re welcome 🙂

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Can I set a limit on the number of characters that appear?’ is closed to new replies.