I have several posts which have similar format of titles. The title format follows like this: "ABC University - Economics" "XYZ University - Social Sciences" "KLM BBB College - Business Administration" so on...
How can I trim the post title until "-". The text after "-" I dont need. I just want get "ABC University" or "XYZ University" or "KLM BBB College".
I cannot do it with wp_trim_words() function as number of words are not same for every post title.
Can anyone help?
EDIT: this code trims post title by 3 words: echo wp_trim_words( get_the_title(), 3, '...'); I want trim post title until hyphen (-)
If you just want to trim title till hyphen (-), then you can use PHP explode for this.
Here is the code which should work for you:
$title = get_the_title();//eg: 'KLM BBB College - Business Administration';
$title_pices = explode("-",$title);
echo $needed_title = trim($title_pices[0]);
Hope this helps!
Use the explode function.
$fullstr = 'XYZ University - Social Sciences';
$splitstr = explode(" - ",$fullstr);
echo $splitstr[0];