我正在开发一个基于Starkers html5 3.0的自定义主题,并使用该主题的评论系统。我遵循了custom\\u form教程Otto, Soapbox Dave, forlogos, 和Deluxe Blog Tips.
我的所有代码似乎都正常运行,只是我无法显示comment\\u form\\u default\\u字段之外的任何字段。此主题设置为子主题,但父主题中还没有注释代码。
你能帮我找到问题吗?
Related functions.php code:
// Custom Callback - Comments
if ( ! function_exists( \'starkers_comment\' ) ) :
/**
* Template for comments and pingbacks.
*
* @since Starkers HTML5 3.0
*/
function starkers_comment( $comment, $args, $depth ) {
$GLOBALS[\'comment\'] = $comment;
switch ( $comment->comment_type ) :
case \'\' :
?>
<article <?php comment_class(); ?> id="comment-<?php comment_ID() ?>">
<?php comment_author_link() ?> <a href="<?php echo esc_url( get_comment_link( $comment->comment_ID ) ); ?>">
<?php echo time_ago(); ?></a> ... <?php comment_reply_link( array_merge( $args, array( \'depth\' => $depth, \'max_depth\' => $args[\'max_depth\'] ) ) ); ?><br />
<?php if ( $comment->comment_approved == \'0\' ) : _e( \'Your comment is awaiting moderation.\', \'starkers\' ); ?><br />
<?php endif;
comment_text(); ?>
<?php
break;
case \'pingback\' :
case \'trackback\' :
?>
<article <?php comment_class(); ?> id="comment-<?php comment_ID() ?>">
<p><?php _e( \'Pingback:\', \'starkers\' ); ?> <?php comment_author_link(); ?><?php edit_comment_link( __(\'(Edit)\', \'starkers\'), \' \' ); ?></p>
<?php
break;
endswitch;
}
endif;
/**
* Closes comments and pingbacks with </article> instead of </li>.
*
* @since Starkers HTML5 3.0
*/
function starkers_comment_close() {
echo \'</article>\';
}
/**
* Adjusts the comment_form() input types for HTML5.
*
* @since Starkers HTML5 3.0
*/
function starkers_fields($fields) {
$commenter = wp_get_current_commenter();
$req = get_option( \'require_name_email\' );
$aria_req = ( $req ? " aria-required=\'true\'" : \'\' );
$fields = array(
\'author\' => \'<p><label for="author">\' . __( \'Name\' ) . \'</label> \' . ( $req ? \'*\' : \'\' ) .
\'<input id="author" name="author" type="text" value="\' . esc_attr( $commenter[\'comment_author\'] ) . \'" size="30"\' . $aria_req . \' /></p>\',
\'email\' => \'<p><label for="email">\' . __( \'Email\' ) . \'</label> \' . ( $req ? \'*\' : \'\' ) .
\'<input id="email" name="email" type="email" value="\' . esc_attr( $commenter[\'comment_author_email\'] ) . \'" size="30"\' . $aria_req . \' /></p>\',
\'url\' => \'<p><label for="url">\' . __( \'Website\' ) . \'</label>\' .
\'<input id="url" name="url" type="url" value="\' . esc_attr( $commenter[\'comment_author_url\'] ) . \'" size="30" /></p>\',
);
return $fields;
}
add_filter(\'comment_form_default_fields\',\'starkers_fields\');
function my_fields($fields) {
$fields[\'new\'] = \'<p>red rover 1</p>\';
return $fields;
}
add_filter(\'comment_form_top\',\'my_fields\');
comments.php code:
<?php if ( post_password_required() ) : ?>
<p><?php _e( \'This post is password protected. Enter the password to view any comments.\', \'starkers\' ); ?></p>
<?php return; endif; ?>
<?php // You can start editing here -- including this comment! ?>
<?php if ( have_comments() ) : ?>
<div class="trirow_875h" id="comments-title"><h4 class="cufon">Activities</h4></div>
<div class="trirow_875m" style="padding:10px 15px;">
<?php if ( get_comment_pages_count() > 1 && get_option( \'page_comments\' ) ) : // Are there comments to navigate through? ?>
<nav>
<?php previous_comments_link( __( \'← Older Comments\', \'starkers\' ) ); ?>
<?php next_comments_link( __( \'Newer Comments →\', \'starkers\' ) ); ?>
</nav>
<?php endif; // check for comment navigation
wp_list_comments( array( \'style\' => \'div\', \'callback\' => \'starkers_comment\', \'end-callback\' => \'starkers_comment_close\' ) );
if ( get_comment_pages_count() > 1 && get_option( \'page_comments\' ) ) : // Are there comments to navigate through? ?>
<nav>
<?php previous_comments_link( __( \'← Older Comments\', \'starkers\' ) ); ?>
<?php next_comments_link( __( \'Newer Comments →\', \'starkers\' ) ); ?>
</nav>
<?php endif; // check for comment navigation
else : // or, if we don\'t have comments:
if ( ! comments_open() ) : ?>
<p><?php _e( \'Comments are closed.\', \'starkers\' ); ?></p>
<?php endif; // end ! comments_open()
endif; // end have_comments()
?>
</div>
<div class="trirow_875b"></div>
<div class="clear"></div>
<?php comment_form(); ?>
Is there anybody out there?
<input type="hidden" name="my_user_id" value="<?php echo $myid; ?>" />
最合适的回答,由SO网友:Otto 整理而成
此代码毫无意义:
function my_fields($fields) {
$fields[\'new\'] = \'<p>red rover 1</p>\';
return $fields;
}
add_filter(\'comment_form_top\',\'my_fields\');
我甚至不知道它应该做什么,因为comment\\u form\\u top是一个动作,而不是一个过滤器。
如果要添加额外字段,应使用comment\\u form\\u default\\u fields筛选器:
add_filter(\'comment_form_default_fields\',\'my_fields\');
但是,由于starkers\\u fields函数的编写不正确,因此这可能不起作用或可能会间歇性地起作用。应该是这样的:
function starkers_fields($fields) {
$commenter = wp_get_current_commenter();
$req = get_option( \'require_name_email\' );
$aria_req = ( $req ? " aria-required=\'true\'" : \'\' );
$fields[\'author\'] = \'<p><label for="author">\' . __( \'Name\' ) . \'</label> \' . ( $req ? \'*\' : \'\' ) .
\'<input id="author" name="author" type="text" value="\' . esc_attr( $commenter[\'comment_author\'] ) . \'" size="30"\' . $aria_req . \' /></p>\';
$fields[\'email\'] = \'<p><label for="email">\' . __( \'Email\' ) . \'</label> \' . ( $req ? \'*\' : \'\' ) .
\'<input id="email" name="email" type="email" value="\' . esc_attr( $commenter[\'comment_author_email\'] ) . \'" size="30"\' . $aria_req . \' /></p>\';
$fields[\'url\'] = \'<p><label for="url">\' . __( \'Website\' ) . \'</label>\' .
\'<input id="url" name="url" type="url" value="\' . esc_attr( $commenter[\'comment_author_url\'] ) . \'" size="30" /></p>\';
);
return $fields;
}
add_filter(\'comment_form_default_fields\',\'starkers_fields\');
这里的区别是,此版本修改字段,而不是直接替换字段。从而允许其他字段不变地通过它。
假设您不想更改starkers\\u fields函数,可能是因为它是您无法控制的父主题,那么您可以通过调整过滤器优先级来解决其中断问题,以确保my\\u fields在它之后运行,如下所示:
function my_fields($fields) {
$fields[\'new\'] = \'<p>red rover 1</p>\';
return $fields;
}
add_filter(\'comment_form_default_fields\',\'my_fields\',20);