Post Reads 0 reads

In this article we are going to see how to get the current post type.

WordPress provide us the function get_post_type() function which return current post type.

E.g.

<div class="post-<?php echo get_post_type(); ?>">

here we have created a CSS class post-<current post type>.

The function get_post_type() internally call the function get_post(). So, we can pass the int|WP_Post as parameter.

All of these parameters are optional. If there is no parameter then it return the current post type.

E.g.

<div class="post-<?php echo get_post_type( get_the_ID() ); ?>">

here we have created a CSS class post-<current post type> from the current post id.

See the details of using function get_the_ID().

Note In this example I have not used any escaping function. Read more about sanitisation and escaping.

Leave a Reply

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

Scroll to Top
%d bloggers like this: