admin管理员组

文章数量:1122846

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.

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.

Share Improve this question edited Sep 1, 2024 at 1:59 Pat J 12.3k2 gold badges28 silver badges36 bronze badges asked Aug 31, 2024 at 19:13 Daniel MartinsDaniel Martins 212 bronze badges 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, 2024 at 2:01
  • Yes, I've already tried the code with the dash, and didnt worked as well – Daniel Martins Commented Sep 1, 2024 at 12:28
Add a comment  | 

2 Answers 2

Reset to default 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
?>

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

本文标签: phpSplit titles by the quotquot in Wordpress