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.
2 Answers
Reset to default 2i'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
版权声明:本文标题:php - Split titles by the " - " in Wordpress 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736296038a1929699.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
-
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. – Pat J Commented Sep 1, 2024 at 2:01