why do i get post id data when i am in homepage

I am trying to create a loop that will recognize the current post id and add a class to it in the menu bar.

this is the code I am using to get the post id value:

$post = $wp_query->post;
$current_project = $post->ID;
echo $current_project; 

this code is working, but the problem is that it also retrieves post id data when I am in the homepage with no posts displaying or queried. It shows the first post's id in order and adds a class to the post's link although it is not currently displayed.

how can I solve this?


In WP Admin, have you set the homepage to a static page? If so then $wp_query->post; will be populated with post data. In your case the homepage will be a post of type 'page'.

Tags: PHP / Wordpress / Post

Similar questions

How can I get ACF data on WooCommerce template-homepage.php
I cannot get my repeater to work on template-homepage.php. I have created a group called 'homepage_settings' and in this group is a repeater called 'categories'. I cannot get this content to show on the template-homepage.php file.
Get data from database table by post_id to get data from second database table
I have two db tables: wp_comments and wp_ratings. I'm trying to create a query using $wpdb which has to do the following: from wp_comments I have to get the comment_ID's where the comment_post_ID is equal to the current post id to get all the ratings from wp_ratings where comment_id is equal to the comment_ID's i got from wp_comments. wp_comments t...
Why is my content missing from homepage?
On my website http://filmblurb.org, all the posts and everything on my home page are missing. Can you anybody tell me how to fix this? Thanks! (If it helps, I'm using Wordpress 3.2.1.)
Why are all pages redirecting to homepage when using custom permalinks?
My website http://laptopre.net/ is running on WP 3.4.2. When I changed the Permalinks setting to Custom Structure - /%category%/%postname%/ , all the pages in my site are redirected to homepage. Could anyone guide me to solve this problem please. Here is content of my .htaccess file Thanks, Nghiep
Why does this loop only work on the homepage?
I've been trying to figure this out all day now. Consider the following code below: The loop above doesn't work on the site's pages (e.g: page 2/3/4) or 404.php, but works perfectly fine on page 1 (the index.php). Why is that and how do I fix this?
Debugging why featured images have stopped showing in WooCommerce on the homepage
At some point, the "Featured" images that usually showed up on our homepage have stopped showing. I've done some digging and found this template code in index.php (this is the Artificer theme by WooThemes) This seems to be the code that handles the featured images: I found out how to grab the last run SQL query, and it comes back as: Sure enough, i...

Also ask

We use cookies to deliver the best possible experience on our website. By continuing to use this site, accepting or closing this box, you consent to our use of cookies. To learn more, visit our privacy policy.