WordPress.org

Support

Support » How-To and Troubleshooting » How do I get a URL formatted version of the title?

How do I get a URL formatted version of the title?

  • I’m trying to use post titles/slugs as image names in my templates. I looked through all of the tags, but I cant find one that outputs the title formatted as a url/slug. (No spaces, all lowercased, etc..)

    Is there a the_slug() tag?

Viewing 7 replies - 1 through 7 (of 7 total)
  • Use urlencode($title) where $title is the text to encode. If you also want it lowercase, use strtolower(urlencode($title))

    hope that helps, adam

    You can use sanitize_title($title) which will get you a slug.

    Could I use this in my template:

    <img src=”/buttons/<?php sanitize_title($title); ?>.gif” />

    Tried it, it’s not returning anything.

    I’m putting it in a category-20 template, right before the call to the_content();


    <div class="entry">

    <img src="/buttons/<?php sanitize_title($title); ?>.gif" />

    <?php the_content('Read the rest of this entry �'); ?>
    </div>

    Try:

    <img src="/buttons/<?php echo sanitize_title($title); ?>.gif" />

    Assuming your variable $title contains a value.

    Assuming your variable $title contains a value.

    And if it doesn’t:

    <img src="/buttons/<?php echo $post->post_name; ?>.gif" />

    This displays the already existing post slug, which is hiding out in the $post object.

    Even better!

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘How do I get a URL formatted version of the title?’ is closed to new replies.