Home > Article > Backend Development > Analyze the post_class and get_post_class functions in WordPress, wordpresspostclass_PHP tutorial
post_class()
post_class is a function built into WordPress for displaying the class name of an article. This function usually generates a unique clss value for each article. If you need to make your own theme and need a little personality, then You'd better stop for a moment. Using this function and flexible css code, we can create a fully 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 are usually displayed directly without returning any value.
<post id="post-<?php the_ID(); ?>" <?php post_class(); ?> > <?php the_content ;?> </post>
Yes, maybe you have noticed that when using the post_class function we don’t even need to write clss="post_class()";.
Example results
Without being too pretentious, the results are as follows
<post id="post-888" class="post-888 post type-post status-publish format-standard hentry category-2 tag-wordpress" > 文章内容 </post>
This is the end of the usage-oriented functions,
The function source code is given below as usual:
If you want to know more about this function and the get_post_class function, please pay attention to 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 detailed explanation
get_post_class is the basic implementation of the post_class function. Like other functions with get in WordPress, this 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 It is encapsulated by generating a unique class value for each article.
If you are a person with low requirements, then the post_class function is actually enough for you. If you are a person with mental mysophobia and don’t want your WordPress website to have too much useless code, 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. The information contained in this array is often used 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 returned array.
The above is my own understanding. Of course, you can also read the official manual.
The content of the rather confusing manual 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 index.php, single.php, 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 there is no need to insert a custom class value, this function does not accept any parameters.
Function parameters
$class: Custom class value, which can be a string or an array.
$post_id: Post ID
Usage examples
$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 Example
$MyClass = get_post_class('index-post',249); //或 $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" }
Summary
According to the source code of the function, we can see that the order of listing the class values of this function is: