Check out this WordPress tutorial if you want to learn how to retrieve the format of a post using a post ID. WordPress has a great function for post format detection; it calls get_post_format. We're going to review this function in a Practical example.
get_post_format function is responsible to retrieve the format of a WP post. This functionality has been added to WordPress since version 3.1.
get_post_format function syntax
This is the syntax when you want to use the
get_post_format function of WordPress (keep reading for the practical example):
get_post_format(int|WP_Post|null $post = null)
If you do not specify the optional
$post parameter for this function, or you just pass
get_post_format will retrieve the post format of the current queried WP post (current post in the loop or in the
Example of using the get_post_format function to retrieve a post’s format in WP
We have specified the ID of the post in
$postId variable. We used
1 as the post ID, you can change it to any post ID you need or do not specify it for the current queried post (the default value for
$post parameter is null).
$postId = 1; $postFormat = get_post_format($postId); if($postFormat) echo $postFormat;
WordPress default Post Formats
WP has its own post formats; also, developers can add new post formats with specifications they desire.
If you did not add any new post formats to WordPress, either by installing third-party plugins or your own codes, This is the list of possible formats (as a “string” that represents the name of the format):
If you try the function on a not -freshly installed WordPress, you may get other formats too.
What do you think about "How to Get Post Format by Post ID in WordPress"?