Make WordPress Core

Opened 11 years ago

Closed 9 years ago

#1271 closed defect (bug) (fixed)

the_permalink() broken outside the loop

Reported by: Skeltoac Owned by:
Milestone: Priority: normal
Severity: minor Version: 1.5
Component: General Keywords:
Focuses: Cc:


The output of the_permalink() on a Page with a loopless template (such as my links page http://www.skeltoac.com/links/) will be something like "http://www.skeltoac.com/2004/04/21//". It should either output the correct URI or nothing at all.

Change History (6)

#1 @Skeltoac
11 years ago

  • Patch set to No

#2 @Skeltoac
11 years ago

A simpler version, not so forgiving or assuming:

if ($post) $idpost = $post;
else return false;

#3 @MC_incubus
11 years ago

You really should be using a loop if you want to use functions like the_permalink(), even if there is only one post.

Alternatively, you can directly pass the ID to the get_permalink() function.

#4 @Skeltoac
11 years ago

It didn't occur to me that I would need a loop in a Page that had no 'post' content. I ran across this bad URI output while writing a plugin; I'm sure others use Page templates without loops, too, so I have to put a workaround in my plugin code. It would just be nice if the_permalink() was more reliable, i.e. didn't spit out invalid URIs.

#5 @Denis de Bernardy
11 years ago

it would also be nice if you could feed it a post, rather than/in addition to an ID

the_permalink() would use $post (global variable)

the_permalink( $ID ) would look in cache, and query the DB if necessary

the_permalink( $post ) would just use whichever info is in $post

#6 @mdawaffe
9 years ago

  • Resolution set to fixed
  • Status changed from new to closed

I just tried this on the default theme's archives page template. It worked.

Note: See TracTickets for help on using tickets.