post_class()

 

File: wp-includes/post-template.php

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

.post {
	/* Styles for all posts */
}

.post-4564 {
	/* Styles for only this post (ID number 4564) */
}

.category-news {
	/* Styles for all posts in the 'news' category  */
}

.format-standard {
	/* Styles for all posts assigned the post-format of 'standard'  */
}

参数:post_class( string|array $class = '', int|WP_Post $post_id = null )

One or more classes to add to the class list. Post ID or post object. Defaults to the global 邮费.

$class

(string|array)
(Optional)
One or more classes to add to the class list.

Default value: ''

$post_id

(int|WP_Post)
(Optional)
Post ID or post object. Defaults to the global $post.

Default value: null

{acontent3}

  1. Add more classes.
    You can add a class to the post_class defaults:

    <div id="post-<?php the_ID(); ?>" <?php post_class( 'class-name' ); ?>>

    The above prints HTML with your added class and the defaults:

    <div id="post-4564" class="class-name post-4564 post type-post status-publish format-standard hentry category-news">

    Use an array to add multiple classes:

    <?php
    $classes = array(
    	'class1',
    	'class2',
    	'class3',
    );
    ?>
    
    <div id="post-<?php the_ID(); ?>" <?php post_class( $classes ); ?>>
    

  2. To print post_class CSS classes for a post other then the current one, specify its ID (integer):

    <?php post_class( '', 22 ); ?>

    The above prints (depending on category and tags):
    class="post-22 post type-post status-publish format-standard hentry category-another-cat tag-tag1 tag-tag2

  3. A simple way to add multiple classes to the post_class defaults, is to just write them as a string argument:

    <div <?php post_class( 'class1 class2 class3' ); ?>>
    

    which will add those classes to the default class list.

  4. Example of the template tag (and its default CSS classes).
    This example shows the post_class template tag as commonly used in a theme file (such as single.php):

    <div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>

    The output of the above prints this HTML (for a post in the ‘news’ category and a theme that supports Post Formats):

    <div id="post-4564" class="post-4564 post type-post status-publish format-standard hentry category-news">

    Using these CSS classes you can then style this specific post, or all posts assigned the same category (or post format):

    .post {
    	/* Styles for all posts */
    }
    
    .post-4564 {
    	/* Styles for only this post (ID number 4564) */
    }
    
    .category-news {
    	/* Styles for all posts in the 'news' category  */
    }
    
    .format-standard {
    	/* Styles for all posts assigned the post-format of 'standard'  */
    }
    

You must log in before being able to contribute a note or feedback.

72.73% (8) 次Baidu访问
9.09% (1) 次Google访问
18.18% (2) 次Sogou访问
  • 蜘蛛:Baidu,8次,更新时间:2019-05-23 06:06:13
  • 蜘蛛:Google,1次,更新时间:2019-04-22 21:55:44
  • 蜘蛛:Sogou,2次,更新时间:2019-05-11 01:54:22

参与评论