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 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.

extract(shortcode_atts(array(
    '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.

share|improve this question
    
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
add comment

2 Answers

up vote 0 down vote accepted

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.

share|improve this answer
add comment

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 -

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.