Skip to main content
deleted 11 characters in body
Source Link

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>";"</span> ";
             unset($ARR_title['0']);
             return $first_word. implode(" ", $ARR_title);
          }
          else
          {
              return "<span>"{$title}</span>";";
          }
       }
       return $title;
    } add_filter( 'the_title', 'add_label_to_post_title' );

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 "<span>{$title}</span>";
          }
       }
       return $title;
    } add_filter( 'the_title', 'add_label_to_post_title' );

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' );
Source Link

Wrap the 2 firsts words of title with a <Span>

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 "<span>{$title}</span>";
          }
       }
       return $title;
    } add_filter( 'the_title', 'add_label_to_post_title' );