get_permalink returning wrong link in excerpt

Problem

In WordPress, the default theme, twenty_eleven, has a function called twentyeleven_continue_reading_link.  This method is supposed to return the permalink for the current excerpt.  However if you use setup_postdata() and call the_excerpt() the permalink will return the current page’s permalink, not the excerpt’s permalink.

 

Solution

The reason it returns the current page’s permalink is because get_permalink() gets the permalink for $post which is the current page.  To resolve this, I looked in setup_postdata to see what it is doing.  setup_postdata is setting a global $id variable each time you call it.  This means that same $id variable is the excerpts id.  Which means in the twentyeleven_continue_reading_link method, instead of calling get_permalink(), you can declare the global $id variable and call get_permalink($id).  See the updated method below.

 

function twentyeleven_continue_reading_link() {
        global $id;
        return ' ' . __( 'Continue reading ', 'twentyeleven' ) . '';
}
Tagged with: , ,

Leave a Reply

Your email address will not be published. Required fields are marked *

*