In this article we are going to see how to get or display the current post id?
Table of Content
WordPress provide us two functions these are:
the_ID() vs get_the_ID() the_ID() vs get_the_ID()
When you want to print the current post ID then use
<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.
When you want to assign the current post id to the variable then you can use the
$post_id = get_the_ID();
Here we have assigned the current post id to the PHP variable