Hi guys, I am having trouble with getting the blog page ID while inside the post. In my custom panel for each page I store a sidebar settings like this $pages[page_id] = sidebar_value = left, right or none. I want to check inside the single.php what is the parent of the post, I tried the
$post->post_parent;
but it returns 0 which means it has no parent. Is there any way to check the parent page ID which holds the posts?
One more thing, when I select blog page in the URL it say’s the page_id=18 but when I echo the page id from the page.php it says that it is = 1 for other pages the ID is current but for blog it doesn’t add up, any idea what am I doing wrong?
You should just do $post->ID or get_the_ID() outside the main loop
$post is not defined outside the loop, get_the_ID doesn’t work… First I need to figure out how can I get blog page id before entering single.php I am not sure why when I do $post->ID it returns the ID of the first post on the page instead of the pages ID.
I assume you use a Blog page template to show your blog posts. If yes, it means that the page is not the post parent.
Usually i use a global variable to get current page id before get_header function. for example:
get_queried_object_id();
get_header();
?>
so, we can call it anywhere…
<?php
global $current_page_id;
echo $current_page_id;
?>
This will work on the page itself, but won’t get you anything when viewing a blog post (single.php) - and that’s what mpc want’s to do. He needs the ID of blog page when viewing a single blog post to decide which sidebar should be displayed.
This will work on the page itself, but won’t get you anything when viewing a blog post (single.php) - and that’s what mpc want’s to do. He needs the ID of blog page when viewing a single blog post to decide which sidebar should be displayed.
If you create a blog posts page from Settings > Reading page, you can get the page id with
This will work on the page itself, but won’t get you anything when viewing a blog post (single.php) - and that’s what mpc want’s to do. He needs the ID of blog page when viewing a single blog post to decide which sidebar should be displayed.
If you create a blog posts page from Settings > Reading page, you can get the page id with
I run into a problem with single.php one more time, I use single.php for blog and portfolio page and I need to somehow check if the parent is blog or portfolio but I cannot get the page id of the parent, do you how any idea how to handle this?
EDIT// What I need exactly is to get the parent page id.
The problem you have here probably based on fact that posts and pages are completely separate post types in WordPress hierarchy. A single blog post doesn’t have any parent (calling it’s parent will return 0). The fact that you query ‘portfolio’ post type on portfolio.php page doesn’t mean anything to WordPress in the means of hierarchy.
If you want to style portfolio and blog posts differently you have to make them separate post types and then use single.php or single-blog.php for blog posts and single-portfolio.php for portfolio posts.
If you want to get the page template that queries your portfolio posts while being on single portfolio post you will have to either get this information from theme options (where user sets which page is a portfolio page) or you will have to query all pages, check the page template the page uses and return the ID of a page that uses portfolio.php page template.
Above solutions won’t work if user wants to have two completely separate blogs, though. For this you could use post meta values on pages.
What is it exactly that you want to achieve by passing values from index.php or portfolio.php to single.php?