1

Working on a project that I'm running into a snag about... I have a page title using the_title()

... that has a location in the title such as this: "Adventure Dental - Colorado Springs, CO"

However, further down on the same page, that title needs to be used again, but this time just with the "Colorado Springs, CO" aspect of the title.

Does anyone know of the best way that I can do this?

2

Hiding part of the title with CSS clipping (as the OP requested in the original unedited title) is almost certainly not the best way to accomplish this. Here is how to do this in the template file.

If all of your titles will be formatted the same way (with a space, a hyphen, and a space), you can do something like this to get just the second part of the title further down in the page:

// Echoes the second part of the post title
$title = get_the_title();
$parts = explode( ' - ', $title );
echo $parts[1];

However, depending on your use case it may be better to have just the business name as the post title, and store the location as a custom field. Then, assuming the location is stored in the custom field named location, you can do something like this in the main title section of your template file:

<h1 class="entry-title">
    <?php the_title(); ?>
    <?php
    if ( $location = get_post_meta( get_the_ID(), 'location', true ) ) {
        echo ' - ' . $location;
    }
    ?>
</h1>
2
  • Thanks for the reply, unfortunately though the first suggestion didn't work and doesn't display anything for the titles at all in my template. Maybe there's something else that's needed? The second suggestion won't really be possible as there's too many pages to make a custom field for these since they've all already been formatted in the same way where "business name - location" is the same exact way that all the pages are formatted. hmm. Really puzzled on how I can get this to work... if you have any ideas please let me know. Thanks again for the reply though. – Mixmastermiike Aug 19 '14 at 15:55
  • Fixed this! You were close in your first suggestion, just needed to have $post->post_title; instead of get_the_title();. Thanks, you're a rockstar. – Mixmastermiike Aug 19 '14 at 16:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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