Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

How can I get an url-friendly version of the post_title? I´m using anchor links for my posts on an archive page, and for the moment the posts get the post_ID as a hash tag but I would like to get the title instead as it would look nicer.

share|improve this question
    
what code is used to generate these anchor links? –  Michael Jun 28 '12 at 22:27
    
I just set the ID of my post to the post_ID. Then I can anchor link to them with a link where the href is #post_ID. No funky code or anything... –  Johan Dahl Jun 29 '12 at 6:00

1 Answer 1

PHP has a function for url encoding that would probably work:

urlencode( get_the_title() );
share|improve this answer
    
Probably had better do: urlencode( get_the_title( $post->ID ) ); –  Pippin Jun 28 '12 at 22:39
    
@pippin's right assuming you're outside of the loop. –  mrwweb Jun 29 '12 at 3:18
    
urlencode( get_the_title() ); gives me: S%C3%A4kerheten+f%C3%B6rst for the title "Säkerheten först". Not really good looking. I´m looking for something which gives me: "sakerheten-forst". –  Johan Dahl Jun 29 '12 at 6:09
    
To get that, I think you'll have to write a regex expression. Sadly, I'm terrible at those. –  mrwweb Jun 29 '12 at 20:17

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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