In this article we are going to see how to get or display the current post id?
Table of Content
Overview Overview
WordPress provide us two functions these are:
- the_ID()
- get_the_ID()
the_ID() vs get_the_ID() the_ID() vs get_the_ID()
the_ID() the_ID()
When you want to print the current post ID then use the_ID()
function.
E.g.
<div class="post-<?php the_ID(); ?>">
here we have created a CSS class post-<current post ID>
.
Note In this example I have not used any escaping function. Read more about sanitisation and escaping.
get_the_ID() get_the_ID()
When you want to assign the current post id to the variable then you can use the get_the_ID()
function.
E.g.
$post_id = get_the_ID();
Here we have assigned the current post id to the PHP variable $post_id
.