kwiz
kwiz

Reputation: 9

Delete two first words and first words in a string (title wordpress)

currently I use tags in my wordpress article titles and place them at the beginning.

https://alerte-prolo.fr/les-alertes-alimentaires/ https://alerte-prolo.fr/alimentaire/acheter-ses-fruits-et-legumes-moins-chers/

I want to keep the tags in the category pages, but in the articles (single.php) I want to delete the tags.

currently I use this to lift the first word of the title.

<h2>
    $originalTitle = the_title('','',false);
    echo substr($originalTitle,strpos($originalTitle, ' '));
</h2>

t works with [Astuce], [gratuit] ect but when I have tag [bon plan] it would be necessary to be able to delete the first two words. I need help for this. thanks

Upvotes: -1

Views: 144

Answers (2)

Yura Rosiak
Yura Rosiak

Reputation: 255

If I understood You correctly:

$originalTitle = "[wtf] [this one to] Some Title";
$newOriginalTitle = preg_replace("/\[[^)]+\]/","",$originalTitle);
echo $newOriginalTitle;

/\[ - defining start

\] - defining end

Upvotes: 0

Alvin
Alvin

Reputation: 875

I think this working for you:

<?php
$originalTitle = "[bon plan] Some Title"; //  In your case: $originalTitle = the_title('','',false);
$newTitle = explode( "] " , $originalTitle, 2);
echo $newTitle[1];
?>

Upvotes: 1

Related Questions