I want to get the current post/page id outside the loop (in header.php) and use this id for Advanced Custom Fields plugin to get the fields.
But i cant get the correct id from front page, archive page(tag, author, category...etc).
Here is the code
if ( is_singular( 'post' ) ) {
$post_id = $wp_query->post->ID;
echo 'post id' . $post_id;
} elseif ( is_shop() ) {
$post_id = get_option( 'woocommerce_shop_page_id' );
echo 'post id' . $post_id;
} else {
$post_id = get_queried_object_id();
echo 'post id' . $post_id
}
One more question, any better method to get the id for all of the page outside loop? I just worry my code is still miss some of the page type.
global $post; echo $post_id = $post->ID;
anywhere will do the job for you. – Mayeenul Islam Nov 22 '15 at 9:04$post
global – Pieter Goosen Nov 22 '15 at 9:08header.php
portion. :( – Mayeenul Islam Nov 22 '15 at 9:09