There\'s a filter for that.
示例:
function wpse_filter_post_class( $classes ) {
// How you determine what class is up to you;
// We will assume you\'ve determined the class name
// and added it to $my_post_class
$my_post_class = \'some-class\';
// Add it to the array of post classes
$classes[] = $my_post_class;
// Return the array
return $classes;
}
add_filter( \'post_class\', \'wpse_filter_post_class\' );
编辑假设您将使用自定义post元数据来确定要添加为post类的布局字符串,您可以执行以下操作:
$my_post_class = ( isset( get_post_meta( get_the_ID(), \'_post_layout\', true ) ? get_post_meta( get_the_ID(), \'_post_layout\', true ) : false );
要将其合并到过滤器回调中,请执行以下操作:
function wpse_filter_post_class( $classes ) {
// Test for your layout post meta
$my_post_class = ( isset( get_post_meta( get_the_ID(), \'_post_layout\', true ) ? get_post_meta( get_the_ID(), \'_post_layout\', true ) : false );
// If it\'s there, use it
if ( $my_post_class ) {
// Add layout as a post class
$classes[] = $my_post_class;
}
// Return the array
return $classes;
}
add_filter( \'post_class\', \'wpse_filter_post_class\' );