Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have a page template which has a loop to show blog posts. Inside the loop, there is get_template_part('content', 'custom') function to include the template (content-custom.php) which shows blog post content. Is it possible to get current page ID inside content-custom.php file?

share|improve this question
    
content-custom.php is not a page, it is basically just the loop that is removed from its original place and added into a separate template. –  Pieter Goosen May 15 at 8:48
    
@PieterGoosen Yes, you are right. But I want to get the ID of a page which is including content-custom.php file by get_template_part() method. –  Sodbileg Gansukh May 15 at 8:55
    
Why do you need the page ID –  Pieter Goosen May 15 at 8:57

4 Answers 4

up vote 0 down vote accepted

You can use all template tags inside this file, so to get the ID, just use get_the_ID() (or the_ID() to output it).

get_the_ID() will retrieve the numeric ID of the current post. It has no parameters and returns the ID of the current post.

the_ID() will display the numeric ID of the current post. It also has no parameters.

If you want to access the ID of page which contains your custom loop, then you could do it like this:

Solution 1 (simple, but not very nice way with global variable)

In your page template:

<?php
    global $parent_page_id;
    $parent_page_id = get_the_ID();
    $paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
    $args = array( 'post_type' => 'post', 'paged' => $paged );
    $temp = $wp_query;
    $wp_query = null;
    $wp_query = new WP_Query( $args );
    $wp_query->query( $args );
?>

<?php if ( $wp_query->have_posts() ) : ?>
    <?php while ( $wp_query->have_posts() ) : $wp_query->the_post(); ?>
    <?php get_template_part( 'content', 'custom' ); ?>
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>

<?php else : ?>
    <?php get_template_part( 'no-results', 'index' ); ?>
<?php endif; ?>

And in your post-content template:

...
global $parent_page_id; // now you can use parent_page_template variable
...

Solution 2 (nicer way with custom query)

In your page template:

<?php
    $paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
    $args = array( 'post_type' => 'post', 'paged' => $paged );
    $my_custom_query = new WP_Query( $args );
?>

<?php if ( $my_custom_query->have_posts() ) : ?>
    <?php while ( $my_custom_query->have_posts() ) : $my_custom_query->the_post(); ?>
    <?php get_template_part( 'content', 'custom' ); ?>
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>

<?php else : ?>
    <?php get_template_part( 'no-results', 'index' ); ?>
<?php endif; ?>

And in your post-content template:

...
... $wp_query->queried_object_id; // it will contain object queried in original wp_query
...
share|improve this answer
    
Thanks Krzysiek, but I want to get the current page ID, not the blog post ID. –  Sodbileg Gansukh May 15 at 8:36
    
OK, so you have a page, which contains custom posts loop and inside that loop you want to access the ID of that page? –  Krzysiek Dróżdż May 15 at 9:09
    
Yes, exactly... –  Sodbileg Gansukh May 15 at 9:10
    
How does that custom loop look like? –  Krzysiek Dróżdż May 15 at 9:12
2  
Simply define a variable outside your custom loop for your current post id that you can re-use later –  passatgt May 15 at 9:21

Per this answer you could use get_queried_object_id() which isn't yet in the Codex, but is related to get_queried_object()

$page_id = get_queried_object_id();
share|improve this answer
    
You really went and dug far and across borders. +1 for miss viking :) –  Pieter Goosen May 15 at 11:09
    
Thanks @PieterGoosen, but I didn't have to dig far... it was the first result I found in Google. –  helgatheviking May 15 at 11:15
    
@helgatheviking But it won't work, since he modifies global $wp_query object. Am I overlooking something? –  Krzysiek Dróżdż May 15 at 11:25
    
@KrzysiekDróżdż I don't think you're overlooking anything. I didn't see mention of that in the question and anyway it seems like a bad idea to modify the global $wp_query. –  helgatheviking May 15 at 11:39
1  
@SodbilegGansukh It seems like you are now asking a new question and should post it as such. And next time, explain what you are doing with the main query so people can give you better advice/answers. I still don't know what you are doing to the main query so I can't comment, but I believe I have answered the question as originally asked. –  helgatheviking May 16 at 16:04

To get page id you can use global post.


global $post;
echo "pageid: ".$post->ID;

Above will retrive current page id not post id.

share|improve this answer
    
I did some Google search before posting here, and tried that already. This is giving me the current blog post ID, but not the page ID inside the template. Thank you shahpranaf. –  Sodbileg Gansukh May 15 at 8:46
    
can you provide front end link ? –  shahpranaf May 15 at 8:48
    
Unfortunately I can't. It's on my local machine. –  Sodbileg Gansukh May 15 at 8:50
    
NP. can you printr $post here? –  shahpranaf May 15 at 8:57
    
Here it is. link –  Sodbileg Gansukh May 15 at 9:01

Solution 1: (Bad Solution)

$p = $GLOBALS['wp_the_query']->get_queried_object_id(); 
//$p will be your page id

Note: You should never use the the global wp_the_query object.

EDIT: if you haven't tampered the global wp_query object, ie: you haven't used query_posts() etc. then helgatheviking's solutions will work: code:

 get_queried_object_id(); 
or 
$GLOBAL['wp_query']->get_queried_object_id();

Solution 2: Like passatgt suggested in the comment, you can store the page id outside the loop and access it inside the loop. the scope issue of accessing it in the template part file can be soloved by declaring it global.

Code:

//1. in the template file befor the loop 

global $myglobal_page_id;
$mygloba_page_id = get_the_ID();
//you can start the loop here
get_template_part('content', 'custom');

// 2. in the template part file 

global $myglobal_page_id; //inside the loop or otherwise

//this will have your page_id

Explanation#Solution 1:

Once inside the loop, the global post object is set to your current post. hence when you call the the_ID(), get_the_ID() (which used get_post() function), you will get the id of current post, and not the main post.

so you need to access the main wp_the_query object (which is a copy of the main query object used for resetting the query ref: wp_reset_query()), as it contains your original query.

share|improve this answer
    
Hmm, I don't think it would work. He modifies global $wp_query in his code. –  Krzysiek Dróżdż May 15 at 10:58
    
it will work, you will be directly accessing the wp_the_query, it's a copy of the main query stored by wordpress. –  MortalViews May 15 at 10:59
    
am editting my answer with more options. –  MortalViews May 15 at 10:59
    
This is not a proper answer. Please edit your answer and add an explanation to what your code does and how it works –  Pieter Goosen May 15 at 10:59
1  
am typing it..:P –  MortalViews May 15 at 11:00

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.