Table of Contents
Analyze the post_class and get_post_class functions in WordPress, wordpresspostclass
Articles you may be interested in:
Home Backend Development PHP Tutorial 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_PHP tutorial

Jul 12, 2016 am 09:01 AM
wordpress

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-<&#63;php the_ID(); &#63;>" <&#63;php post_class(); &#63;> > <&#63;php the_content ;&#63;> </post>
Copy after login

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>
Copy after login

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 ) ) . '"';
}

Copy after login

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

<&#63;php get_post_class($class, $post_id); &#63;>
Copy after login

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);
Copy after login

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"
}
Copy after login

Advanced Example

$MyClass = get_post_class('index-post',249);
//或 
$MyClass = get_post_class(array( 'index-post'),249);
 var_dump($MyClass);
Copy after login

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"
}
Copy after login

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

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1088784.htmlTechArticleAnalyze the post_class and get_post_class functions in WordPress. wordpresspostclass post_class() post_class is a built-in class in WordPress for displaying articles. A function with a name, which is passed...
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP vs. Flutter: The best choice for mobile development PHP vs. Flutter: The best choice for mobile development May 06, 2024 pm 10:45 PM

PHP and Flutter are popular technologies for mobile development. Flutter excels in cross-platform capabilities, performance and user interface, and is suitable for applications that require high performance, cross-platform and customized UI. PHP is suitable for server-side applications with lower performance and not cross-platform.

How to change page width in wordpress How to change page width in wordpress Apr 16, 2024 am 01:03 AM

You can easily modify your WordPress page width by editing your style.css file: Edit your style.css file and add .site-content { max-width: [your preferred width]; }. Edit [your preferred width] to set the page width. Save changes and clear cache (optional).

In which folder are wordpress articles located? In which folder are wordpress articles located? Apr 16, 2024 am 10:29 AM

WordPress posts are stored in the /wp-content/uploads folder. This folder uses subfolders to categorize different types of uploads, including articles organized by year, month, and article ID. Article files are stored in plain text format (.txt), and the filename usually includes its ID and title.

How to create a product page in wordpress How to create a product page in wordpress Apr 16, 2024 am 12:39 AM

Create a product page in WordPress: 1. Create the product (name, description, pictures); 2. Customize the page template (add title, description, pictures, buttons); 3. Enter product information (stock, size, weight); 4 . Create variations (different colors, sizes); 5. Set visibility (public or hidden); 6. Enable/disable comments; 7. Preview and publish the page.

Where is the wordpress template file? Where is the wordpress template file? Apr 16, 2024 am 11:00 AM

WordPress template files are located in the /wp-content/themes/[theme name]/ directory. They are used to determine the appearance and functionality of the website, including header (header.php), footer (footer.php), main template (index.php), single article (single.php), page (page.php), Archive (archive.php), category (category.php), tag (tag.php), search (search.php) and 404 error page (404.php). By editing and modifying these files, you can customize the appearance of your WordPress website

How to search for authors in WordPress How to search for authors in WordPress Apr 16, 2024 am 01:18 AM

Search for authors in WordPress: 1. Once logged in to your admin panel, navigate to Posts or Pages, enter the author name using the search bar, and select Author in Filters. 2. Other tips: Use wildcards to broaden your search, use operators to combine criteria, or enter author IDs to search for articles.

Which version of wordpress is stable? Which version of wordpress is stable? Apr 16, 2024 am 10:54 AM

The most stable WordPress version is the latest version because it contains the latest security patches, performance enhancements, and introduces new features and improvements. In order to update to the latest version, log into your WordPress dashboard, go to the Updates page and click Update Now.

What language is used to develop WordPress? What language is used to develop WordPress? Apr 16, 2024 am 12:03 AM

WordPress is developed using PHP language as its core programming language for handling database interactions, form processing, dynamic content generation, and user requests. PHP was chosen for reasons including cross-platform compatibility, ease of learning, active community, and rich library and frameworks. Apart from PHP, WordPress also uses languages ​​like HTML, CSS, JavaScript, SQL, etc. to enhance its functionality.

See all articles