Opened 5 years ago

Closed 5 years ago

#6345 closed enhancement (worksforme)

Function to return single post / page ID

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

Description

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)

Why not this?

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

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.

  • 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.