{"id":673,"date":"2011-12-01T18:24:32","date_gmt":"2011-12-02T04:24:32","guid":{"rendered":"http:\/\/mymonkeydo.com\/?p=673"},"modified":"2020-05-03T16:44:11","modified_gmt":"2020-05-04T02:44:11","slug":"get_permalink-returning-wrong-link-in-excerpt","status":"publish","type":"post","link":"https:\/\/mymonkeydo.com\/get_permalink-returning-wrong-link-in-excerpt\/","title":{"rendered":"get_permalink returning wrong link in excerpt"},"content":{"rendered":"
In WordPress, the default theme, twenty_eleven, has a function called\u00a0twentyeleven_continue_reading_link. \u00a0This method is supposed to return the permalink for the current excerpt. \u00a0However if you use\u00a0setup_postdata() and call the_excerpt() the permalink will return the current page’s permalink, not the excerpt’s permalink.<\/p>\n
<\/p>\n
The reason it returns the current page’s permalink is because get_permalink() gets the permalink for $post which is the current page. \u00a0To resolve this, I looked in setup_postdata to see what it is doing. \u00a0setup_postdata is setting a global $id variable each time you call it. \u00a0This means that same $id variable is the excerpts id. \u00a0Which means in the\u00a0twentyeleven_continue_reading_link method, instead of calling get_permalink(), you can declare the global $id variable and call get_permalink($id). \u00a0See the updated method below.<\/p>\n
<\/p>\n
function twentyeleven_continue_reading_link() {\n global $id;\n return ' ' . __( 'Continue reading