WordPress.org

Support

Support » How-To and Troubleshooting » Explode function on the_title

Explode function on the_title

  • abovegrnd
    Member

    @abovegrnd

    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>
Viewing 1 replies (of 1 total)
  • abovegrnd
    Member

    @abovegrnd

    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?

Viewing 1 replies (of 1 total)
  • The topic ‘Explode function on the_title’ is closed to new replies.