Divide titles by the " – " in WordPress

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 global $my_query; $post_number = ($my_query->current_post + 1); $zero_padding = str_pad($post_number, 2, '0', STR_PAD_LEFT); ?> <?php $title = get_the_title(); // Get the title string $token = strtok($title, '-'); // Split the title by the " – " separator $artist = $token; // Get the artist $song = strtok('–'); // 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 titles-top10"><?php echo $artist; ?></h6> <h5 class="mt-2 titles-top10"><?php echo $song; ?></h5> </div> </div> I've already changed the $token = strtok($title, '-'); but still it wont work.

Comment (1)

Jese Leos

August 29, 2024

Verified user

start from adding one missing </div> closing tag: <div class="div-titles"> seems not closed.

You’ll be in good company