Retrieving the post ID of the current post in WordPress is quite simple, thanks to built-in WordPress functions.
Here’s a step-by-step guide:
-
Identify the Template File: First, you need to identify where you want to display or use the post ID. It could be in your theme’s single post template (
single.php
), in the loop in your main blog page template (index.php
), or somewhere else depending on your needs. -
Insert the Post ID Function: Open the appropriate template file and find the place within the WordPress Loop where you want to display or use the post ID. Then, insert the following line of code:
<?php echo 'Post ID: ' . get_the_ID(); ?>
This line of code will print the text ‘Post ID: ‘ followed by the ID of the current post.
-
Save and Upload Your Changes: After you’ve added this line, save your changes to the file. If you’re editing the file offline, upload it back to your server using an FTP client.
-
Check Your Site: Now, you can visit your site and navigate to a post where you’ve added the code to see the post ID.
Remember to have a backup of your files before you make any changes. Also, if you aren’t comfortable editing your theme files, you may want to ask for assistance from a developer.