Skip to main content
added 52 characters in body
Source Link
freejack
  • 606
  • 3
  • 7

You can do that like this:

function add_label_to_post_title( $title = '' ) {
    global $post;

    if( 'post' == $post->post_type && trim( $title ) != "" ){
        $title_words = explode( " ", $title );
        $word_count = count( $title_words );
        
        //Sets how many words should be wrapped
        $words_to_wrap = 2;
        $last_word_index = $word_count > $words_to_wrap ? $words_to_wrap - 1 : $word_count - 1;

        $title_words[0] = '<span>' . $title_words[0];
        $title_words[$last_word_index]$title_words[ $last_word_index ] = $title_words[$last_word_index]$title_words[ $last_word_index ] . '</span>';

        $title = implode( ' ', $title_words );
 
    }
    return $title;
}
add_filter( 'the_title', 'add_label_to_post_title' );

You can change the value of $words_to_wrap to choose how many words should be wrapped in the span element. If a title has less words than $words_to_wrap value it would wrap only the available ones.

You can do that like this:

function add_label_to_post_title( $title = '' ) {
    if( trim( $title ) != "" ){
        $title_words = explode( " ", $title );
        $word_count = count( $title_words );
        
        //Sets how many words should be wrapped
        $words_to_wrap = 2;
        $last_word_index = $word_count > $words_to_wrap ? $words_to_wrap - 1 : $word_count - 1;

        $title_words[0] = '<span>' . $title_words[0];
        $title_words[$last_word_index] = $title_words[$last_word_index] . '</span>';

        $title = implode( ' ', $title_words );
 
    }
    return $title;
}
add_filter( 'the_title', 'add_label_to_post_title' );

You can change the value of $words_to_wrap to choose how many words should be wrapped in the span element. If a title has less words than $words_to_wrap value it would wrap only the available ones.

You can do that like this:

function add_label_to_post_title( $title = '' ) {
    global $post;

    if( 'post' == $post->post_type && trim( $title ) != "" ){
        $title_words = explode( " ", $title );
        $word_count = count( $title_words );

        //Sets how many words should be wrapped
        $words_to_wrap = 2;
        $last_word_index = $word_count > $words_to_wrap ? $words_to_wrap - 1 : $word_count - 1;

        $title_words[0] = '<span>' . $title_words[0];
        $title_words[ $last_word_index ] = $title_words[ $last_word_index ] . '</span>';

        $title = implode( ' ', $title_words );
    }
    return $title;
}
add_filter( 'the_title', 'add_label_to_post_title' );

You can change the value of $words_to_wrap to choose how many words should be wrapped in the span element. If a title has less words than $words_to_wrap value it would wrap only the available ones.

Source Link
freejack
  • 606
  • 3
  • 7

You can do that like this:

function add_label_to_post_title( $title = '' ) {
    if( trim( $title ) != "" ){
        $title_words = explode( " ", $title );
        $word_count = count( $title_words );
        
        //Sets how many words should be wrapped
        $words_to_wrap = 2;
        $last_word_index = $word_count > $words_to_wrap ? $words_to_wrap - 1 : $word_count - 1;

        $title_words[0] = '<span>' . $title_words[0];
        $title_words[$last_word_index] = $title_words[$last_word_index] . '</span>';

        $title = implode( ' ', $title_words );

    }
    return $title;
}
add_filter( 'the_title', 'add_label_to_post_title' );

You can change the value of $words_to_wrap to choose how many words should be wrapped in the span element. If a title has less words than $words_to_wrap value it would wrap only the available ones.