To print post_class CSS classes for a post other then the current one, specify its ID (integer):
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
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):
The output of the above prints this HTML (for a post in the ‘news’ category and a theme that supports Post Formats):
[html]
[/html]
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' */
}
var wporgFunctionReferenceI18n = {“copy”:”Copy”,”copied”:”Code copied”,”expand”:”Expand code”,”collapse”:”Collapse code”,”sourceFile”:”wp-includes/post-template.php”};
//# sourceURL=wporg-developer-function-reference-js-extra
var quicktagsL10n = {“closeAllOpenTags”:”Close all open tags”,”closeTags”:”close tags”,”enterURL”:”Enter the URL”,”enterImageURL”:”Enter the URL of the image”,”enterImageDescription”:”Enter a description of the image”,”textdirection”:”text direction”,”toggleTextdirection”:”Toggle Editor Text Direction”,”dfw”:”Distraction-free writing mode”,”strong”:”Bold”,”strongClose”:”Close bold tag”,”em”:”Italic”,”emClose”:”Close italic tag”,”link”:”Insert link”,”blockquote”:”Blockquote”,”blockquoteClose”:”Close blockquote tag”,”del”:”Deleted text (strikethrough)”,”delClose”:”Close deleted text tag”,”ins”:”Inserted text”,”insClose”:”Close inserted text tag”,”image”:”Insert image”,”ul”:”Bulleted list”,”ulClose”:”Close bulleted list tag”,”ol”:”Numbered list”,”olClose”:”Close numbered list tag”,”li”:”List item”,”liClose”:”Close list item tag”,”code”:”Code”,”codeClose”:”Close code tag”,”more”:”Insert Read More tag”};
//# sourceURL=quicktags-js-extra
var wporg_note_preview = {“ajaxurl”:”https://developer.wordpress.org/wp-admin/admin-ajax.php”,”nonce”:”119215fa41″,”preview”:”preview note”,”preview_empty”:”Nothing to preview”,”is_admin”:””};
//# sourceURL=wporg-developer-preview-js-extra
Skip to note 6 content
hearvox
Add more classes.
You can add a class to the
post_classdefaults:<div id="post-<?php the_ID(); ?>" <?php post_class( 'class-name' ); ?>>The above prints HTML with your added class and the defaults:
[html]
Use an array to add multiple classes:
<div id="post-<?php the_ID(); ?>" <?php post_class( $classes ); ?>>Skip to note 7 content
hearvox
To print
post_classCSS classes for a post other then the current one, specify its ID (integer):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-tag2Skip to note 8 content
hearvox
Example of the template tag (and its default CSS classes).
This example shows the
post_classtemplate tag as commonly used in a theme file (such assingle.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):
[html]
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' */ }Skip to note 9 content
Selrond
A simple way to add multiple classes to the
post_classdefaults, 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.
Skip to note 10 content
hearvox
post_class filter
You can also add classes using the
post_classfilter.You must log in before being able to contribute a note or feedback.