Robin Majumdar
Robin Majumdar

Reputation: 1

Wordpress : Remove everything after a specific character

So there is a good example on how to remove everything before specific character, but I'm looking for opposite... after a specific character, in the WordPress title field.

What I have: A WordPress title - stuff I want to delete

What I want: A WordPress title

I've experimented with the code (which is almost exactly what I want to do) in this example Wordpress, remove everything before a specific character but I'm doing something wrong with my code! And yes I added the code to the functions.php file to recognize the dash as the special character.

Thanks!

Upvotes: 0

Views: 555

Answers (1)

Ohgodwhy
Ohgodwhy

Reputation: 50787

Simple. Explode on the delimiter, use the parts of the array.

$parts = explode(' - ', get_the_title(), 2);
$before = $parts[0]; //before the - 
$after = $parts[1]; //after the - 

because you're discussing the_title and Wordpress, I'm going to provide the integration as a filter as well:

function explode_parts($title, $id){
    $parts = explode(' - ', $title);
    $before = $parts[0]; //before the - 
    $after = $parts[1]; //after the - 
    return (whatever);
}

add_filter('the_title', 'explode_parts');

Make sure to add a conditional statement so that the function is only executed on titles that you wish to perform this operation on.

Upvotes: 1

Related Questions