0

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?

<?php
$Path=$_SERVER['REQUEST_URI'];
$URI= get_bloginfo('url').$Path; // get the current web address for my WP site
?>

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

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

0

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)

0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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