Get first word in string php the_title WordPress

I’m trying to shorten a wordpress title to just the first word. For a page named “John Doe” I want to have a sub title somewhere on the page that says “About John” so I want to just get the first word from the title.

Is there a way to do this with PHP?

Read More

Thanks!

UPDATE:
Thanks for your answers! I tried the following code but it doesn’t seem to be working. It still echoes the full title. Any suggestions?

<?php   
   $title = the_title();
   $names = explode(' ', $title);
   echo $names[0];
?>

Related posts

Leave a Reply

7 comments

  1. this is very easy:

    <?php
    $title = get_the_title(); // This must be!, because this is the return - the_title would be echo
    $title_array = explode(' ', $title);
    $first_word = $title_array[0];
    
    echo $first_word;
    ?>
    

    or

    <?php
    $title = current(explode(' ', get_the_title()));
    
    echo $title;
    ?>
    

    untested, but should work 🙂

    Hope this is helpful 🙂

  2. $first_word = current(explode(' ', $title  ));
    

    or in your template file

    <?php echo current(explode(' ', $title  )) ?>
    

    explode by space and get first element in resulting array

  3. I think you’re probably running into some of the idiosyncrasies of WordPress here. the_title(), by default, just prints out the title and returns nothing. In order to make it return the title string instead of printing it, you have to set the third parameter of the_title() to true.

    //fetch the title string into a variable
    $title = the_title('','',true);
    

    Once you have it, then you can get rid of the first word and echo it manually. If you’re using PHP 5.3, you can accomplish this in one line with strstr():

    echo strstr($title," ",true);
    

    If you have PHP < 5.3, you can use explode():

    $parts = explode(' ',$title);
    echo $parts[0];
    
  4. I would recommend avoiding this string parsing entirely and using a more general approach for defining a secondary title for your page.

    You should use WordPress’ Post Meta system to define a custom meta field with a name like ‘subtitle’, then call that field in your template. That way you can decide on a page-by-page basis what you want the subtitle to be, rather than being locked into a specific relationship between post title and subtitle. This probably won’t make your life more complicated and it will simplify it significantly.

    You add post meta to a page or post using the “Custom Fields” section at the bottom of the post editing screen. You display those fields in your theme like this:

    <?php echo get_post_meta($post->ID, $meta_key, 1);?>
    

    Obviously you’d probably be better off checking it exists first, giving you something like this:

    <?php if ( $subtitle = get_post_meta($post->ID, $meta_key, 1) ) 
        echo "<h3>$subtitle</h3>";
    ?>
    

    More details on the codex.

  5. There is a string function (strtok) which can be used to split a string into smaller strings (tokens) based on some separator(s). For the purposes of this thread, the first word (defined as anything before the first space character) of Test me more can be obtained by tokenizing the string on the space character.

    <?php
    $value = "Test me more";
    echo strtok($value, " "); // Test
    ?>
    

    For more details and examples, see the strtok PHP manual page.