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

WBOY
Release: 2016-07-12 09:01:35
Original
1131 people have browsed it

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...
Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template