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?
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?
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!
This topic has been closed to new replies.