SoFunction
Updated on 2025-03-09

Parsing post_class and get_post_class functions in WordPress

post_class()
post_class is a built-in function in WordPress for displaying the name of an article class. This function usually generates a unique clss value for each article. If you need to create your own theme and need a little personality, then you'd better stop and use this function and use it with flexible css code to create a personalized WordPress blog.

post_class function description
This function usually generates a unique clss value for each article, which can be easily used in the node where the article is located.

Function usage
Like other WordPress tag functions such as header_image and wp_title, functions without get will usually be displayed directly without returning any value.

<post  <?php post_class(); ?> > <?php the_content ;?> </post>

Yes, maybe you have noticed that we don't even need to write clss="post_class()" like this when using the post_class function;.

Example results
If you don't keep it, the result is as follows

&lt;post  class="post-888 post type-post status-publish format-standard hentry category-2 tag-wordpress" &gt; Article content &lt;/post&gt;

The main function is finished.
The function source code is given below:
If you want to know more about this function and the get_post_class function, please follow the later articles.

/**
 * Display the classes for the post div.
 *
 * @since 2.7.0
 *
 * @param string|array $class One or more classes to add to the class list.
 * @param int $post_id An optional post ID.
 */
function post_class( $class = '', $post_id = null ) {
 // Separates classes with a single space, collates classes for post DIV
 echo 'class="' . join( ' ', get_post_class( $class, $post_id ) ) . '"';
}

get_post_class details
get_post_class is the basic implementation of the post_class function. Like some other functions with get in WordPress, the function will have a return value, and the return value will be an array containing the basic information of the current article. The get_post_class function is mainly used to generate a unique class value for each article and is encapsulated.

If you are a person with low requirements, then the post_class function is actually enough for you tossing. If you are a person with a mental cleanliness and don't want too much useless code on your WordPress website, then you can continue reading.

Detailed explanation of get_post_class function
This function is mainly used to generate an array of information related to the current article. We often use the information contained in this array as the class value in the article layer.
Just like the post_class function I mentioned above, it uses the class value generated by this function.
And this function supports inserting your own class values ​​and merging them into the return array.
The above is my own understanding, of course you can also read the official manual.

The more puzzling manual content is as follows:
WordPress Themes have a template tag for the post HMTL tag which will help theme authors to style more effectively with CSS. The Template Tag is called get_post_class. This function returns different post container classes which can be added, typically, in the , , and other template files featuring post content, typically in the HTML

tag.
Function usage

<?php get_post_class($class, $post_id); ?>

If it is in a loop and does not need to insert a custom class value, the function can not accept any parameters.

Function parameters
$class: Custom class value, which can make strings or dead arrays.

$post_id: Article ID

Examples of usage

$MyClass = get_post_class(); 
 var_dump($MyClass);

Output result:

array(9) {
 [0]=>
 string(8) "post-249"
 [1]=>
 string(4) "post"
 [2]=>
 string(9) "type-post"
 [3]=>
 string(14) "status-publish"
 [4]=>
 string(15) "format-standard"
 [5]=>
 string(6) "hentry"
 [6]=>
 string(18) "category-catcatcat"
 [7]=>
 string(8) "tag-tag1"
 [8]=>
 string(8) "tag-tag2"
}

Advanced examples

$MyClass = get_post_class('index-post',249);
//or$MyClass = get_post_class(array( 'index-post'),249);
 var_dump($MyClass);

Output result:

array(10) {
 [0]=>
 string(8) "post-249"
 [1]=>
 string(4) "post"
 [2]=>
 string(9) "type-post"
 [3]=>
 string(14) "status-publish"
 [4]=>
 string(15) "format-standard"
 [5]=>
 string(6) "hentry"
 [6]=>
 string(18) "category-catcatcat"
 [7]=>
 string(8) "tag-tag1"
 [8]=>
 string(8) "tag-tag2"
 [9]=>
 string(10) "index-post"
}

Summarize
According to the source code of the function, we can see that the order in which the class values ​​of this function are listed is:

  • Article id
  • Article type (page, article)
  • The article type (page, article) is the same as the previous one, but the result is the word ‘type-’
  • Publish status
  • Article format
  • Is a password required
  • The categories described in the article (the categories will be listed one by one)
  • The tags described in the article (the tags will be listed one by one)