WordPress.org

Forums

Explode function on the_title (2 posts)

  1. abovegrnd
    Member
    Posted 5 years ago #

    I have a section of my website where we post new tracks from various artists. I want to take the title of those tracks and break them down into "artist" and "song" so that I can style them differently and use them in various places in my template.

    It should be a simple process; all of the post titles are formatted like you might expect ("Artist - Song name")

    For some reason, no matter what I do I cannot split this string (the_title) at the dash (" - ") and return anything of value.

    I used a combination of strpos() and subsr(), I tried strstr(), and I've now tried using explode() -- and for some reason it's no good.

    If you look at the code, $firstName[0] SHOULD be the artist name (occurring before " - ") but for some reason it just returns the entire title.
    When I used strstr() and strpos/substr it just returned blank. Someone please help me, I'm pulling my hair out!

    <ul style="border-top: 1px solid #9e9e9e; width:250px; float: right; padding: 0; margin-right: 5px;">
    <span style="background-color: #9e9e9e; color: #FFF; font-weight: bold; width: 250px;">NEW JOINTS</span>
    <?php query_posts('showposts=10&offset=0&cat=562'); ?>
    <?php while (have_posts()) : the_post('', '', FALSE); ?>
    <li style="padding: 10px 5px; border-bottom:1px solid #9e9e9e; font-size: small; list-style-type: none;">
    <?php
    $firstName = explode(" – ",the_title('','',false));
    echo $firstName[0];
    ?>
    </li>
    <?php endwhile; ?>
    </ul>
  2. abovegrnd
    Member
    Posted 5 years ago #

    After further testing it seems that it works with any other delimiter other than "-" or " - ". So, it seems to have trouble picking up the dash in the middle, anyone have any idea why that might be?

Topic Closed

This topic has been closed to new replies.

About this Topic