// 自定义默认头像 add_filter( 'avatar_defaults', 'zzjbzlz_avatar' ); function zzjbzlz_avatar ($avatar_defaults) { $myavatar = get_bloginfo('template_directory') . '/img/zzjbzlz_avatar.gif'; // OR --> $myavatar = "http://zzjb.leiling.org/wp-content/uploads/2017/05/default_avatar.gif"; $avatar_defaults[$myavatar] = "重装机兵资料站"; return $avatar_defaults; }
标签归档:wordpress
wordpress评论函数
调用评论
comment_form()常用在comments.php中,调用整个评论区
<?php comment_form($args, $post_id); ?>
$args:comment_form() 的输出配置参数,为一个关联数组,配置项非常丰富,下面我们会详细说明。
$post_id:文章id,默认为空,即当前id
$args的默认配置:
$defaults = array( 'fields' => apply_filters( 'comment_form_default_fields', $fields ), 'comment_field' => ' <label for="comment">' . _x( 'Comment', 'noun' ) . '</label><textarea id="comment" name="comment" cols="45" rows="8" aria-required="true"></textarea> ', 'must_log_in' => ' ' . sprintf( __( 'You must be <a href="%s">logged in</a> to post a comment.' ), wp_login_url( apply_filters( 'the_permalink', get_permalink( $post_id ) ) ) ) . ' ', 'logged_in_as' => ' ' . sprintf( __( 'Logged in as <a href="%1$s">%2$s</a>. <a href="%3$s" title="Log out of this account">Log out?</a>' ), admin_url( 'profile.php' ), $user_identity, wp_logout_url( apply_filters( 'the_permalink', get_permalink( $post_id ) ) ) ) . ' ', 'comment_notes_before' => ' ' . __( 'Your email address will not be published.' ) . ( $req ? $required_text : '' ) . ' ', 'comment_notes_after' => ' ' . sprintf( __( 'You may use these <abbr title="HyperText Markup Language">HTML</abbr> tags and attributes: %s' ), ' <code>' . allowed_tags() . '</code>' ) . ' ', 'id_form' => 'commentform', 'id_submit' => 'submit', 'title_reply' => __( 'Leave a Reply' ), 'title_reply_to' => __( 'Leave a Reply to %s' ), 'cancel_reply_link' => __( 'Cancel reply' ), 'label_submit' => __( 'Post Comment' ), );
如果需要修改表单,需要在function.php中使用过滤器comment_form_default_fields
删除表单字段
如果我们想要删除网址字段,只需要打开主题的 functions.php 文件,添加以下代码:
add_filter(‘comment_form_default_fields’, ‘mytheme_remove_url’);
function mytheme_remove_url($arg) {
$arg[‘url’] = ”;
return $arg;
}
保存后刷新页面,你就会看到“url”输入框已经不存在了。
新增表单字段
假设我们要添加一个 QQ 字段,同样在主题的 functions.php 添加下面的代码即可:
add_filter('comment_form_default_fields', 'my_fields'); function my_fields($fields) { $fields['qq'] = ' ' . '<label for="qq">'.__('QQ').'</label> ' . '<input id="qq" name="qq" type="text" value="' . esc_attr( $commenter['comment_qq'] ) . '" size="30" /> '; return $fields; }
刷新页面,即可看到新增的表单。
替换默认表单字段
代码和上面的例子差不多,如果你设置的字段为(author、email、url)其中之一,即 $fields[‘author’]、$fields[’email’]、$fields[‘url’] ,就可以替换默认的字段的输出内容。
默认的这三个字段如下:
add_filter('comment_form_default_fields', 'mytheme_remove_url'); function my_fields($fields) { $fields = array( 'author' => ' ' . '<label for="author">' . __( 'Name' ) . '</label> ' . ( $req ? '<span class="required">*</span>' : '' ) . '<input id="author" name="author" type="text" value="' . esc_attr( $commenter['comment_author'] ) . '" size="30"' . $aria_req . ' /> ', 'email' => ' <label for="email">' . __( 'Email' ) . '</label> ' . ( $req ? '<span class="required">*</span>' : '' ) . '<input id="email" name="email" type="text" value="' . esc_attr( $commenter['comment_author_email'] ) . '" size="30"' . $aria_req . ' /> ', 'url' => ' <label for="url">' . __( 'Website' ) . '</label>' . '<input id="url" name="url" type="text" value="' . esc_attr( $commenter['comment_author_url'] ) . '" size="30" /> ', ); }
需要注意的是,wordpress原生只支持author、email、url,如果新增或修改前台表单,还需要对评论审核后台、数据库等做对应改造。
WordPress不同页面显示不同侧边栏(插件与非插件)
WordPress支持小工具,小工具组成侧边栏,但是大多数主题都是整个网站共用一个侧边栏,这往往让一些有特殊需求的人头疼。比如,你想在某个页面的侧边栏上单独投放广告,你想在某个页面的侧边栏显示特定的超链接,或者你只是想DIY一些特定的图片和文字,等等。
这里有实现WordPress在不同页面显示不同小侧边栏的两种方法。