• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

My Monkey Do

A Log of Coding Solutions

  • Home
  • Web Hosts
  • Tools
  • About

get_permalink returning wrong link in excerpt

December 1, 2011 by Webhead

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' ) . '';
}

Filed Under: Coding Tagged With: cms, php, wordpress

Primary Sidebar

Topics

apache apple block editor chrome cms css debug eCommerce embed firebug firefox git gmail goDaddy google hosting htaccess html html 5 IE crap image iPad iPhone javascript jquery linux localization mac os x ms sql mysql open source optimize php php 5.3 responsive rest api seo svg tinymce woocommerce wordpress wpengine xss yii youtube




Categories

  • Coding
  • Off the Shelf
  • Plugins
  • Random Thoughts
  • Server Stuff
  • Tools