0

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.

2
  • 3
    Are you sure that the - in the title returned by get_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.
    – Pat J
    Commented Sep 1 at 2:01
  • Yes, I've already tried the code with the dash, and didnt worked as well Commented Sep 1 at 12:28

2 Answers 2

2

i've already solved it, i had to change the $title = get_the_title(); to $title = get_post()->post_title;

in the end it looks like that:

<?php
$title = get_post()->post_title; // Get the title string
$parts = explode(' - ', str_replace('–', '-', $title)); // Replace em dash with en dash and split the title

$artist = trim($parts[0]); // Get the artist and trim any extra spaces
$song = trim($parts[1]); // Get the song title and trim any extra spaces
?>
1
  • 1
    OK, that makes sense. get_the_title() will run the content through WordPress's the_title filter. Some quick testing shows me that ` - ` gets filtered to ` &8211; , but -` is left as -. I'm not sure if that's a built-in WordPress filter or something being done by a plugin or my theme, but either way, using get_post()->post_title as you are will bypass all the filters and give you the raw title text.
    – Pat J
    Commented Sep 2 at 17:51
1

If the code is performing correctly when $title is set to a known string, but then not working when set dynamically from the get_the_title() function, this does suggest that the "-" character is actually an encoded entity. This would explain why the explode(' – ', $title); line is not working as intended.

A similar issue is discussed in this thread. I suggest adding in the html_entity_decode() function to ensure that "-" is actually a "-" like so:

<?php
    $title = html_entity_decode( 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>

I also took the liberty of adding in default values just in case no - is found 👍.

3
  • i've already solved it, i had to change the '$title = get_the_title();' to '$title = get_post()->post_title;' Commented Sep 2 at 12:06
  • thanks anyways! Commented Sep 2 at 12:08
  • Hi Daniel. Glad to hear it! Accessing the post_title property directly is most likely bypassing the character encoding issue altogether. Just be aware you are now working with a "raw" unfiltered and possible unsanitized value. You may want to consider using a sanitization function such as wp_kses_post() when echoing into your template file. Commented Sep 4 at 1:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.