views:

550

answers:

8

Hi,

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?

Thanks!

+1  A: 
$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

Galen
@Galen: +1 for using the `current` :)
Sarfraz
+1 for giving me a +1!
Galen
+1  A: 
list($first_word) = explode(' ',$mystring);
oezi
+1 for teaching me about `list()`.
Sebastian P.
adding a trim() make it more reliable.
takpar
A: 

You can do like this:

$words = explode(' ', $the_title_here);
echo $words[0];
Sarfraz
A: 

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];
?>
j-man86
@j-man - It's better to edit your question with updated information, rather than post the update as an answer. You should delete this answer and update your question instead.
zombat
view source of your title. maybe the spaces are written as  
takpar
A: 

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];
zombat
Or, you can use `strtok($title, ' ')`
salathe
A: 

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 :)

ahmet2106
Thanks! Works perfect! The key here is to use get_the_title, the_title won't work for this purpose.
j-man86
A: 

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.

jeremyclarke
I forgot to mention that this is a page for a client. If there's one thing this client hates, it's having the responsibility to define more parameters in the admin. The string parsing makes it so that I DON'T have to make him fill in extra information. The subtitle is just for one page and always works the same way. If the title of the page is John Doe, then the subtitle will read "About John". Anyway, the issue is all taken care of– thanks for your help!
j-man86
A: 

Hi, I would like to change color of the first word of title. Could you help me with it. Thanks in advance.

Leon