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.

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
add comment

2 Answers

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
add comment

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
add comment

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.