I am displaying the titles of the top articles. I want to show only the first word of the title, whatever the legth of this word is. When I use trimming it shows the second words' first letters. When I activate Trim only on a word boundary it does not show some words because of max length. Some of the first words are 5 letters some of 13 letters.

I want to show only whole first words. How can I do this?

Comments

preper’s picture

I am not a coder so I don't now much about codes. But there is no place for a php code in views section.. Can you explain me how to use, or which codes to use?

johnhanley’s picture

Include the following code in a field views theme file:

$words = explode(' ', $title);
print $words[0];

$title is the string containing the node title. Season to taste.

preper’s picture

I have added this code to views-view-fields.tpl.php but there is no change. There is an advice 'use strpos() to find the first space, and then substr()' but I don't know how to use them. I can't develop codes. But just need code will command that: ''show field until a space''.

Thanks for help.

johnhanley’s picture

Using strpos() and substr() is just an alternate way to accomplish the same thing. The previous way shown is actually simpler and more reliable.

But that's not why the code isn't working. You'll need to investigate how to create views field theme to manipulate the default output.

bamban’s picture

The codes that you have suggested didn't work beacuse of html tags if I use strip_tags(); before exploding it works but I need html output that is cut first word of title is there another way to do that ? by the way $title variable is not avaliable to use in views theming just gives $output variable. strpos() and substr() don't make sense explode() function is best choice though

$words = explode(' ', $output);
print $words[0];

stephenrobinson’s picture

you need to add $words = explode(' ', $title); print $words[0]; to the node tpl file, not the view? depends if your view is a block or page, if it is a block, you must fix the node file.

bamban’s picture

it is a block that I work given the code just works for node pages not work views theming $title variable is not avaliable to use in views theming system it just provides those $view,$field,$row,$output so on I would like to cut block links that creates with views not node pages thanks your interest droopysteve

ellieelectrons’s picture

Thank you for this thread. I found it helpful. I had a two word title on my website and I wanted the colour of each word to be different. What I wanted to do was a bit similar to this styleshout template:
http://www.styleshout.com/templates/preview/BrightSide11/index.html

I used the explode command to do this in page.tpl.php:

 $words = explode(' ', $site_name);
		print $words[0]; 

print $words[1];

I'm guessing this isn't the best way to do it though... as if I change my site's name to be more than two words then the rest of the words won't display. What is the best way of doing it? I'm presuming I might need a for... in... each statement or something... My php is a bit rusty so I was wondering if anyone can advise or point me to an appropriate link.

Thanks.

Ellie.