WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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

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
@Johan why not use the slug then? – Jonathan Joosten Feb 4 at 10:52

If you want the short version with "-" between the words use the slug. If you don't want to use the slug use the function bellow:

    $new_url = sanitize_title(get_the_title());
    echo $new_url;


share|improve this answer

Your Answer


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.