2

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?

3
  • 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. May 15, 2014 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. May 15, 2014 at 8:55
  • Why do you need the page ID May 15, 2014 at 8:57

4 Answers 4

2

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
...
9
  • Thanks Krzysiek, but I want to get the current page ID, not the blog post ID. May 15, 2014 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? May 15, 2014 at 9:09
  • Yes, exactly... May 15, 2014 at 9:10
  • How does that custom loop look like? May 15, 2014 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, 2014 at 9:21
10

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();
10
  • 1
    You really went and dug far and across borders. +1 for miss viking :) May 15, 2014 at 11:09
  • 1
    Thanks @PieterGoosen, but I didn't have to dig far... it was the first result I found in Google. May 15, 2014 at 11:15
  • @helgatheviking But it won't work, since he modifies global $wp_query object. Am I overlooking something? May 15, 2014 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. May 15, 2014 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. May 16, 2014 at 16:04
1

To get page id you can use global post.


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

Above will retrive current page id not post id.

5
  • 1
    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. May 15, 2014 at 8:46
  • can you provide front end link ?
    – shahpranaf
    May 15, 2014 at 8:48
  • Unfortunately I can't. It's on my local machine. May 15, 2014 at 8:50
  • NP. can you printr $post here?
    – shahpranaf
    May 15, 2014 at 8:57
  • Here it is. link May 15, 2014 at 9:01
0

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.

5
  • Hmm, I don't think it would work. He modifies global $wp_query in his code. May 15, 2014 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. May 15, 2014 at 10:59
  • am editting my answer with more options. May 15, 2014 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 May 15, 2014 at 10:59
  • 1
    am typing it..:P May 15, 2014 at 11:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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