WordPress Get Post ID

Advertisement

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()

Top ↑

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.

Top ↑

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.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: