我正在使用 BEM 方法创建一个 WordPress 主题以供公开发布。为了实现 BEM 方法,我删除/修改了一些默认的 WordPress 模板和类。例如,为了在帖子评论列表中实现 BEM 方法,我创建了自定义 walker 类:
<?php
// Walker class used to create an HTML list of comments.
class BEM_Walker_Comment extends Walker_Comment {
// Starts the list before the elements are added.
public function start_lvl( &$output, $depth = 0, $args = array() ) {
$GLOBALS['comment_depth'] = $depth + 1;
$output .= '<ol class="comment__children">' . "\n";
}
// Ends the list of items after the elements are added.
public function end_lvl( &$output, $depth = 0, $args = array() ) {
$GLOBALS['comment_depth'] = $depth + 1;
$output .= "</ol>\n";
}
// Starts the element output.
public function start_el( &$output, $comment, $depth = 0, $args = array(), $id = 0 ) {
$depth++;
$GLOBALS['comment_depth'] = $depth;
$GLOBALS['comment'] = $comment;
if ( ! empty( $args['callback'] ) ) {
ob_start();
call_user_func( $args['callback'], $comment, $args, $depth );
$output .= ob_get_clean();
return;
}
if ( ( 'pingback' === $comment->comment_type || 'trackback' === $comment->comment_type ) && $args['short_ping'] ) {
ob_start();
$this->ping( $comment, $depth, $args );
$output .= ob_get_clean();
} else {
ob_start();
$this->html5_comment( $comment, $depth, $args );
$output .= ob_get_clean();
}
}
// Ends the element output, if needed.
public function end_el( &$output, $comment, $depth = 0, $args = array() ) {
if ( ! empty( $args['end-callback'] ) ) {
ob_start();
call_user_func( $args['end-callback'], $comment, $args, $depth );
$output .= ob_get_clean();
return;
}
$output .= "</li>\n";
}
// Outputs a comment in the HTML5 format.
protected function html5_comment( $comment, $depth, $args ) {
?>
<li <?php $this->comment_class( $this->has_children ? 'comment__parent' : '', $comment ); ?>>
<article class="comment__body">
<footer class="comment__meta">
<div class="comment__author">
<?php
if ( 0 !== $args['avatar_size'] ) {
echo get_avatar( $comment, $args['avatar_size'] );
}
$url = get_comment_author_url( $comment );
$author = get_comment_author( $comment );
if ( empty( $url ) || 'http://' === $url ) {
$author_name = $author;
} else {
$author_name = '<a class="comment__author-url" href="' . esc_url( $url ) . '" rel="external nofollow">' . esc_html( $author ) . '</a>';
}
printf( '<b class="comment__author-name">%s</b>', $author_name );
?>
</div>
<div class="comment-metadata">
<a href="<?php echo esc_url( get_comment_link( $comment, $args ) ); ?>">
<time datetime="<?php comment_time( 'c' ); ?>">
<?php printf( __( '%1$s at %2$s', 'runway' ), get_comment_date( '', $comment ), get_comment_time() ); ?>
</time>
</a>
<?php edit_comment_link( __( 'Edit', 'runway' ), '<span class="comment__edit">', '</span>' ); ?>
</div>
<?php if ( '0' === $comment->comment_approved ) : ?>
<p class="comment-awaiting-moderation"><?php esc_html_e( 'Your comment is awaiting moderation.', 'runway' ); ?></p>
<?php endif; ?>
</footer>
<div class="comment__content">
<?php comment_text(); ?>
</div>
<?php
comment_reply_link( array_merge( $args, array(
'add_below' => 'div-comment',
'depth' => $depth,
'max_depth' => $args['max_depth'],
'before' => '<div class="comment__reply">',
'after' => '</div>',
) ) );
?>
</article>
<?php
}
// Generates semantic classes for each comment element.
protected function comment_class( $class = '', $comment = null, $post_id = null, $echo = true ) {
$classes = join( ' ', get_comment_class( $class, $comment, $post_id ) );
$classes = str_replace( ' byuser', ' comment--by-user', $classes );
$classes = str_replace( ' comment-author-',' comment--author-', $classes );
$classes = str_replace( ' bypostauthor', ' comment--by-post-author', $classes );
$classes = str_replace( ' odd', ' comment--odd', $classes );
$classes = str_replace( ' alt', ' comment--alt', $classes );
$classes = str_replace( ' even', ' comment--even', $classes );
$classes = str_replace( ' thread-odd', ' comment--thread-odd', $classes );
$classes = str_replace( ' thread-alt', ' comment--thread-alt', $classes );
$classes = str_replace( ' thread-even', ' comment--thread-even', $classes );
$classes = str_replace( ' depth-', ' comment--depth-', $classes );
// Separates classes with a single space, collates classes for comment DIV.
$class = 'class="' . $classes . '"';
if ( $echo ) {
echo $class;
} else {
return $class;
}
}
} // BEM_Walker_Comment class
?>
同样,我还为导航菜单创建了 walker 类,并修改了评论表单以实现 BEM 方法。
但是删除/修改默认的 WordPress 类和模板安全吗?
但是删除/修改默认的 WordPress 类和模板安全吗?
是的!
我将 BEM 用于我的 WordPress 模板,并从 WordPress 中删除了所有原始 CSS 类。我们唯一需要保留的是:
id="s"
用于搜索字段;id="com-{commentId}"
,WordPress 的 JS 助手使用它来移动“回复”字段。