0

I would like to enclose the 2 first word of title inside a to get the following result:

TOP 5 of best photos

I tried using the code below, but it returns only the first word. How can i do to select 2 words?

Thank you

    function add_label_to_post_title( $title = '' ) {
       if(trim($title) != "")
       {
      $ARR_title = explode(" ", $title);

      if(sizeof($ARR_title) > 1 )
          {
             $first_word = "<span>".$ARR_title['0']."</span> ";
             unset($ARR_title['0']);
             return $first_word. implode(" ", $ARR_title);
          }
          else
          {
              return "{$title}";
          }
       }
       return $title;
    } add_filter( 'the_title', 'add_label_to_post_title' );

1 Answer 1

0

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.

6
  • Hello. Thank you for this. It works well. But is there a way to make it apply only to post title? Not on page... Or if it's not possible to select post can i add somewhere before a condition that the text has a minimum 4 words. I tried to add this before: if( sizeof($title) > 3 ) but it's not working.
    – Stuart66
    Commented Sep 11, 2019 at 17:35
  • Hi, I have updated the code to limit the function to posts only.
    – freejack
    Commented Sep 11, 2019 at 20:46
  • Super! Apreciated your help!
    – Stuart66
    Commented Sep 11, 2019 at 21:13
  • Hello. I have a new question. I noticed it was also adding the wrap in admin and also in title from gamify rank. From admin, I found the way to hide, but ti avoid it wrap ranks, how can I add information that the filter apply only when there are more than 2 words? Thank you for your help
    – Stuart66
    Commented Apr 20, 2020 at 12:51
  • Hi, What is gamify rank? Are the titles you want to add the span inside the loop only? If so just add if( ! in_the_loop() ){ return $title; } at the top of the function.
    – freejack
    Commented Apr 20, 2020 at 17:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.