返回介绍

post_class()

发布于 2017-09-11 09:56:13 字数 4045 浏览 1225 评论 0 收藏 0

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

Display the classes for the post div.


description


参数

$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


源代码

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

更新日志

Versiondescription
2.7.0Introduced.

相关函数

Uses

  • wp-includes/post-template.php: get_post_class()

User Contributed Notes

  1. Skip to note content You must log in to vote on the helpfulness of this noteVote results for this note: 0You must log in to vote on the helpfulness of this note Contributed by hearvox

    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'  */
    }
    
  2. 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 ); ?>>
    

    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

    post_class filter
    You can also add classes using the post_class filter.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文