WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

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

I use the_permalink() to pass the current page to the facebook like button. But when one goes directly to a comment link with a hashtag, such as example.com/post#comment1, it's considered a separate link and FB likes are counted separately. How can I have the_permalink() point to just example.com/post and strip the #comment1?

share|improve this question

echo get_permalink($post);, or the_permalink() in the Loop context, will give you the canonical URL for the post, without the trailing hashtag part-- technically the 'fragment'.

You could also use PHP's parse_url to break the URL apart if *_permalink functions don't work for you for some reason, or even PHP's strpos and substr for a really homegrown solution.

share|improve this answer

Everything after the # is not sent per HTTP. The browser will keep this information in memory to use the value on the target page. In PHP you cannot get this value, because PHP is running on the server.

Use a client-side script language like JavaScript.

share|improve this answer

Your Answer


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