I have a problem.

I'm using a custom shortcode to try to get the url. If the shortcode is inside a post then it gets the post url. When I open the post on a category list, this post shortcode works correctly.

    'gurl'  => get_permalink(),
), $atts));

But when i try to add this shortcode in widget text, I get the last post url.

Tell me please how I can get the post url when the shortcode is in a widget.

  • Is this for archive pages, single views, both? – kaiser May 19 '12 at 10:25
  • @mrwweb this problem have place on archive page – Kaiser May 22 '12 at 7:25

Depending on how the page's query and loop are constructed, when you access the post object from the sidebar or footer, you sometimes get the last post in the loop rather than the page itself.

You can ensure you're getting the original (page) object by running wp_reset_query() from inside your widget (before you access the post object or run any functions that assume the current post as their target).

One caveat here: make sure there's nothing after your widget that would be affected by this post reset.


get_permalink() without a Post ID specified should be used inside The Loop.

You can use it outside The Loop, but you have to pass an ID (get_permalink($id)).

How you get that ID is up to you, and dependant on your exact scenario.

For further information, please see the following -

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.