


Analyze the post_class and get_post_class functions in WordPress, wordpresspostclass_PHP tutorial
Analyze the post_class and get_post_class functions in WordPress, wordpresspostclass
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:
- Article id
- Post type (page, post)
- The article type (page, article) is the same as the previous one, but the word ‘type-’ is added in the result
- Release Status
- Article Format
- Do you need a password
- The categories mentioned in the article (the categories will be listed one by one)
- The tags mentioned in the article (the tags will be listed one by one)
Articles you may be interested in:
- Analysis of the use of PHP functions used to obtain recent articles in WordPress development
- Introduction to the use of PHP functions related to custom menus in WordPress development
- Usage analysis of the PHP function used to obtain the search form in WordPress
- Use the wp_count_posts function in WordPress to count the number of articles
- Detailed explanation of calling comment templates and looping output comments in WordPress PHP function
- Explanation of simple steps to add Google search function in WordPress
- Detailed explanation of get_post and get_posts functions in WordPress development
- Get_post_custom() function in WordPress development Usage analysis
- Install and use the video player plug-in Hana Flv Player in WordPress
- Detailed explanation of the use of classification function wp_list_categories in WordPress
- Implementation of short codes and related functions in WordPress development Tips for use

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



There is no perfect WordPress product registration plugin, the choice should be based on actual needs and website size. Recommended plug-ins include: MemberPress: powerful but high-priced, complex configuration Restrict Content Pro: Focus on content restrictions and member management, cost-effective Easy Digital Downloads: Sell digital products, and users register as additional functions

Reasons why WordPress websites cannot be found in search engines: 1. Indexing issues; 2. Content issues; 3. Website technical issues; 4. Link issues; 5. Other issues such as geographical restrictions, website name and social media presence.

A step-by-step guide to replacing a header image of WordPress: Log in to the WordPress dashboard and navigate to Appearance >Theme. Select the topic you want to edit and click Customize. Open the Theme Options panel and look for the Site Header or Header Image options. Click the Select Image button and upload a new head image. Crop the image and click Save and Crop. Click the Save and Publish button to update the changes.

How to turn off a comment in WordPress? Specific article or page: Uncheck Allow comments under Discussion in the editor. Whole website: Uncheck "Allow comments" in "Settings" -> "Discussion". Using plug-ins: Install plug-ins such as Disable Comments to disable comments. Edit the topic file: Remove the comment form by editing the comments.php file. Custom code: Use the add_filter() function to disable comments.

WordPress Error Resolution Guide: 500 Internal Server Error: Disable the plug-in or check the server error log. 404 Page not found: Check permalink and make sure the page link is correct. White Screen of Death: Increase the server PHP memory limit. Database connection error: Check the database server status and WordPress configuration. Other tips: enable debug mode, check error logs, and seek support. Prevent errors: regularly update WordPress, install only necessary plugins, regularly back up your website, and optimize website performance.

How to copy WordPress code? Copy from the admin interface: Log in to the WordPress website, navigate to the destination, select the code and press Ctrl C (Windows)/Command C (Mac) to copy the code. Copy from a file: Connect to the server using SSH or FTP, navigate to the theme or plug-in file, select the code and press Ctrl C (Windows)/Command C (Mac) to copy the code.

Enable comments in WordPress website: 1. Log in to the admin panel, go to "Settings" - "Discussions", and check "Allow comments"; 2. Select a location to display comments; 3. Customize comments; 4. Manage comments, approve, reject or delete; 5. Use <?php comments_template(); ?> tags to display comments; 6. Enable nested comments; 7. Adjust comment shape; 8. Use plugins and verification codes to prevent spam comments; 9. Encourage users to use Gravatar avatar; 10. Create comments to refer to

To log in to a WordPress website account: Visit the login page: Enter the website URL plus "/wp-login.php". Enter your username and password. Click "Login". Verification Two-step Verification (optional). After successfully logging in, you will see the website dashboard.
