An example of getting the current page / post ID, identifying whether the current item is a page or a post, and then appending the results to the content.
All from within a plugin.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | <?PHP /* Plugin Name: Get Page / Post ID using a plugin by D.Busby Saiweb.co.uk Plugin URI: http://saiweb.co.uk Description: Identifies the current page/post and appends text to the content Version: 0.1 Author: David Busby Author URI: http://saiweb.co.uk */ //WP hooks start add_filter('the_content', 'post_page'); //WP hooks end function post_page($content){ global $post; //wordpress post global object if($post->post_type == 'page'){ $content .= '<br /> This item is a page and the ID is: '.$post->ID; } elseif($post->post_type == 'post') { $content .= '<br /> This item is a post and the ID is: '.$post->ID; } return $content; } ?> |
Install the above as a plugin i.e. in wp-content/plugins/test/test.php
Head over to your admin menu and enable the plugin, now each page and post will identify itself as a page or post, and provide it’s ID.
There is a lot available in the $post object for a list add
1 2 3 4 |
Entries (RSS)
or
get_the_ID() is handy for templates aswell in conjunction with the get_page and get_post functions.
Sean
Sean is correct, however I have a slight adjustment.
The $page isn’t an array, it is an object. In this sense, using print_r (print array) probably isn’t the best option. Consider using var_dump as an alternative OR you can actually access the object properties directly like this:
$post->ID
This also allows you to set this as a variable for use outside the loop, e.g.
$curr_post_id = $post->ID
I am using this to imporve the WordPress pagination function on a site.