Geting page ID on a single.php


#1

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?

#2

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?


#3

You should just do $post->ID or get_the_ID() outside the main loop


#4
turkhitbox said

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.


#5

add " global $post; " at the top and try again.


#6

The thing here is that the single blog post doesn’t have any parent, so you won’t get the blog page’s ID this way.

The $post -> post_parent will only work with hierarchical post types, that is, for example, a page which is a direct child of another page.

How is your blog page set up? Is it a custom page template which queries blog posts or is it a page specified in reading settings of WordPress?


#7

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… :wink:

<?php
global $current_page_id;
echo $current_page_id;
?>

#8
agusmu said

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… :wink:

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


#9
pogoking said

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

<?php echo get_option('page_for_posts'); ?>

#10
$pages = get_pages();
foreach ( $pages as $page ) {
    if ( is_page_template( 'blog.php' ) {
        $parent_id = $page -> ID;
    }
}

This should do the trick if you use custom page template for as blog page. I hope that is_page_template works this way.

EDIT: Or what agusmu suggested above :slight_smile:


#11
agusmu said
pogoking said

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

<?php echo get_option('page_for_posts'); ?>

Thanks that works! :slight_smile:


#12

Thanks for all the answers, I got it working :wink: I wonder what if someone wants to have few blogs? :stuck_out_tongue:


#13

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.


#14

Is there a way to pass some value from portfolio.php or index.php to single.php?


#15

Seriously, no one? So how do you guys do this?


#16

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?