What happens when an element has multiple class attributes?
当一个属性被声明为多个单个元素(这是无效的HTML,顺便说一下),行为方面,第一个值将覆盖相同属性的所有后续值。所以在这种情况下,你的元素只会有一两三个类。
这个行为在the HTML5 spec, 8.2.4.35 Attribute name state中解释,“…如果[element]上已经有一个具有完全相同名称的属性,那么这是一个解析错误,新属性必须被删除…”
If you know the correct way of adding a class to this snippet (WordPress plugin), then that would also be appreciated!
通常情况下,如果您需要动态地将自定义类添加到WordPress的帖子中,则可以挂接到post_class过滤器并根据需要操作$ classes数组。这在我的主题中大致如下:
function nv_post_class( $classes ) {
// Most recent post on the front page
global $count;
if ( is_home() && 1 == $count )
$classes[] = 'latest-post';
return $classes;
}
add_filter( 'post_class', 'nv_post_class' );
如果只需要添加一个或多个静态类,则将它们作为空格分隔的字符串直接传递给post_class():