Skip to content

Instantly share code, notes, and snippets.

@jaclyntan
Created August 13, 2019 09:00
Show Gist options
  • Save jaclyntan/855ccf013efa5c9da15b32e6f1d4a798 to your computer and use it in GitHub Desktop.
Save jaclyntan/855ccf013efa5c9da15b32e6f1d4a798 to your computer and use it in GitHub Desktop.
Get the ID from posts, pages, and archives in WordPress
if ( is_home() && ! in_the_loop() ) {
$ID = get_option('page_for_posts');
} elseif ( is_post_type_archive()) {
$query = get_queried_object();
$slug = $query->name;
$pageobj = get_page_by_title($slug);
$ID = $pageobj->ID;
} else {
$ID = get_the_ID();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment