I wanted to divide a Wordpress post title by the hyphen, in PHP and HTML in a Wordpress page for different div's.
Here's an example of the title i wanna divide "Charli XCX ft. Billie Eilish - Guess". I wanna separate the "Charli XCX ft. Billie Eilish" of the "Guess".
Here's the code I'm using, that i cant make work.
<?php
$title = get_the_title(); // Get the title string
$parts = explode(' – ', $title); // Split the title by the " - " separator
$artist = $parts[0]; // Get the artist
$song = $parts[1]; // Get the song title
?>
<div class="col center-elements-top10">
<div class="img-top10" style="background-image: url(<?php the_post_thumbnail_url() ?>)!important;"></div>
<div class="div-titles">
<h5 class="number-top10"><?php echo $zero_padding; ?></h5>
<div>
<h6 class="mt-2 artist-top10"><?php echo $artist; ?></h6>
<h5 class="mt-2 song-top10"><?php echo $song; ?></h5>
</div>
</div>
</div>
If I edit the title to "Charli XCX ft. Billie Eilish-Guess" it works. But I want it with the space between the hyphen, just like that "Charli XCX ft. Billie Eilish - Guess". Also I've tried to change the code to that and put the title fix, just like that $title = "Charli XCX ft. Billie Eilish – Guess";
, and it worked, but when I changed to $title = get_the_title();
stops dividing.
-
in the title returned byget_the_title()
is a hyphen, and hasn't been changed (by WordPress or some other means, eg, copy and pasting from a word processing program) into an en dash?–
vs-
can be hard to distinguish visually, but they're two different characters, and PHP will only match the one you're asking it to match.