Make WordPress Core

Opened 10 years ago

Closed 10 years ago

#6345 closed enhancement (worksforme)

Function to return single post / page ID

Reported by: Viper007Bond Owned by:
Milestone: Priority: normal
Severity: normal Version: 2.5
Component: General Keywords:
Focuses: Cc:


Proposal for a new function for use outside of The Loop:

If you're on a single post or page (i.e. is_single() or is_page() is TRUE), this function would return the ID of that post or page, otherwise it'd return NULL or whatever.

Now before you suggest it, global $post; echo $post-ID; is unreliable because it can be thrown off by other Loops such as the "Recent Posts" sidebar widget.

Change History (3)

#1 @Otto42
10 years ago

Why not this?

if (is_single()) $id = get_query_var('p'); else if (is_page()) $id = get_query_var('page_id');

#2 @markjaquith
10 years ago

See get_queried_object_id(); But note that it will work on more than is_singular() pages. So you'd want to still do your is_singular() check.

#3 @markjaquith
10 years ago

  • Keywords needs-patch removed
  • Milestone 2.7 deleted
  • Resolution set to worksforme
  • Status changed from new to closed
Note: See TracTickets for help on using tickets.