WordPress.org

Ready to get started?Download WordPress

Forums

How do I get a URL formatted version of the title? (8 posts)

  1. nolageek
    Member
    Posted 8 years ago #

    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?

  2. adamh
    Member
    Posted 8 years ago #

    Use urlencode($title) where $title is the text to encode. If you also want it lowercase, use strtolower(urlencode($title))

    hope that helps, adam

  3. pizdin_dim
    Member
    Posted 8 years ago #

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

  4. nolageek
    Member
    Posted 8 years ago #

    Could I use this in my template:

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

  5. nolageek
    Member
    Posted 8 years ago #

    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>

  6. pizdin_dim
    Member
    Posted 8 years ago #

    Try:

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

    Assuming your variable $title contains a value.

  7. Kafkaesqui

    Posted 8 years ago #

    "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.

  8. pizdin_dim
    Member
    Posted 8 years ago #

    Even better!

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags