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'm trying to find out the PHP code that will get the current URL and add a hash ID onto the end of it so a link can be generated and linked to.

The end goal is to create a link that returns: http://mydomain.com/news#postID-123. This should a) take users to the news page and b) jump to the part of the news page where the post ID of 123 is displayed.

Ideally I need a solution that can handle pagination within Wordpress, this is my code to get the curent page URL that I spotted on another thread but how do I create a new string from it?

$URI= get_bloginfo('url').$Path; // get the current web address for my WP site
share|improve this question

migrated from stackoverflow.com May 29 '12 at 21:47

This question came from our site for professional and enthusiast programmers.

up vote 0 down vote accepted

Your answer should return: `` so from there you just do this:

$final_url = $URI.'#'.get_the_ID();

Which should echo http://mydomain.com/current/page#372 which is kinda good for linking. But if your looking for a "bookmarks" link it would be best practice to do this (inside the loop):

echo '<a href="'.the_permalink().'" title="'.the_title().'">'.the_title.'</a>';

Which will echo "Interesting Page" (note the title attribute gets stripped by the SE engine)

share|improve this answer
This is working a treat however it outputs a trailing slash on the end of the URL, before the post ID, this is what my outputted code looks like: localhost:8888/wp_thedistrict/wp_thedistrict/news/#145 how can I remove this last slash between news and #145? – egr103 May 30 '12 at 15:08
The trailing slash shouldn't be an issue but if you use get_permalink() to store the_permalink() into a variable then use <a href="php.net/manual/en/function.strpos.php">strpos</a>; to get the offset then <a href="php.net/manual/en/function.substr.php">substr</a>; to trim everything from 0 to offset (possibly offset - or + 1) then use the output of that in favour of just straight the_permalink() – Joshua Jun 1 '12 at 0:53
I hope that made sense, and for whatever reason the linking didn't work properly. – Joshua Jun 1 '12 at 0:54

Try this:

$jump_url = sprintf ('%s#PostID-%s', site_url('/news'), get_the_ID() );

Pagination is trickier. For any given post, you'd have to figure out what page it renders on each time you generate this link.

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.