远方的海
远方的海,WordPress,免费
2014 Sep 24 18 : 32

WordPress回复添加博主认证和评论等级

您当前的位置: 首页 > WordPress > 正文
欢迎光临远方的海!您可能还对 WordPress建站免费资源最新IT动态 感兴趣,点击开启 悦读 模式。CTRL+D 快捷收藏本文,我们期待您再次光临!
[文章目录]

如大家所见,远方的海下面的评论已经增加了博主的验证图标和评论等级图标。只要在本站回复数超过3个,就会显示Vip1的标记。而且博主验证也很酷啊!

这个灵感是和畅想资源的博主AreFly学的,可以增加用户黏度,也美观。接下来给出实现的方案~

添加functions.php代码

首先先上一个样式的截图:

WordPress回复添加博主认证和评论等级

打开主题的functions.php,添加以下代码:

//远方的海获取访客VIP样式
//请自行修改管理员的邮箱
function get_author_class($comment_author_email,$comment_author_url){
global $wpdb;
$adminEmail = 'svensproject@gmail.com';
$author_count = count($wpdb->get_results(
"SELECT comment_ID as author_count FROM $wpdb->comments WHERE comment_author_email = '$comment_author_email' "));
if($comment_author_email ==$adminEmail)
echo '<a class="vip7" title="评论超人就是你!"></a>';
$linkurls = $wpdb->get_results(
"SELECT link_url FROM $wpdb->links WHERE link_url = '$comment_author_url'");
if($author_count>=3 && $author_count<10 && $comment_author_email!=$adminEmail)
echo '<a class="vip1" title="评论达人 LV.1"></a>';
else if($author_count>=10 && $author_count<20 && $comment_author_email!=$adminEmail)
echo '<a class="vip2" title="评论达人 LV.2"></a>';
else if($author_count>=20 && $author_count<30 && $comment_author_email!=$adminEmail)
echo '<a class="vip3" title="评论达人 LV.3"></a>';
else if($author_count>=30 && $author_count<50 && $comment_author_email!=$adminEmail)
echo '<a class="vip4" title="评论达人 LV.4"></a>';
else if($author_count>=50 &&$author_count<80 && $comment_author_email!=$adminEmail)
echo '<a class="vip5" title="评论达人 LV.5"></a>';
else if($author_count>=80 && $author_coun<200 && $comment_author_email!=$adminEmail)
echo '<a class="vip6" title="评论达人 LV.6"></a>';
else if($author_count>=200 && $comment_author_email!=$adminEmail)
echo '<a class="vip7" title="评论达人 LV.7"></a>';
foreach ($linkurls as $linkurl) {
if ($linkurl->link_url == $comment_author_url )
echo '<a class="vp" target="_blank" href="/links/" title="哟!隔壁邻居的呢!"></a>';
}
}

原理是根据评论人的Email,查询他的评论数量,来判断对应的VIP等级,这个代码很多地方可以定制的,比如样式可以自己定义,也可以设置所达到等级需要的评论数目。

调用函数

在适合的地方调用此函数,因为每个主题基本都不一样,所以需要大家找到你的主题调用评论的地方,搜索<?php comment_author_link();?>在后面加上

<?php get_author_class($comment->comment_author_email,$comment->user_id)?>

如果需要添加博主认证则,在上述代码后面加上:

<?php get_author_class($comment->comment_author_email,$comment->user_id)?>
<?php if(user_can($comment->user_id, 1)){echo "<a title='博主认证' class='vip'></a>";}; ?>

增加CSS样式

style.css中加上以下代码,并把vip.png上传到主题的images目录下,就大功告成了!

/**博客VIP评论样式**/
.vp,.vip,.vip1,.vip2,.vip3,.vip4,.vip5,.vip6,.vip7{background: url(images/vip.png) no-repeat;display: inline-block;overflow: hidden;border: none;}
.vp{background-position:-495px -3px;width: 14px;height: 14px;margin-bottom: -2px;}
.vip{background-position:-515px -2px;width: 15px;height: 16px;margin-bottom: -3px;}
.vip1{background-position:-1px -2px;width: 46px;height: 14px;margin-bottom: -1px;}
.vip2{background-position:-63px -2px;width: 46px;height: 14px;margin-bottom: -1px;}
.vip3{background-position:-144px -2px;width: 46px;height: 14px;margin-bottom: -1px;}
.vip4{background-position:-227px -2px;width: 46px;height: 14px;margin-bottom: -1px;}
.vip5{background-position:-331px -2px;width: 46px;height: 14px;margin-bottom: -1px;}
.vip6{background-position:-441px -2px;width: 46px;height: 14px;margin-bottom: -1px;}
.vip7{background-position:-611px -2px;width: 46px;height: 14px;margin-bottom: -1px;}
下载vip.png,右击另存为即可:

WordPress回复添加博主认证和评论等级

以上。

好文!分享给朋友,或者点个赞吧~

文章信息

分类:WordPress

您可能也会喜欢

发表回复

Post Comment


  1. 花臣 :

    不知为何 按照以上 方法 弄了 但是 看不到 效果

    2016-6-18
  2. Songdaoyuan :

    这个没有找到

    2016-6-7
  3. Songdaoyuan :

    您好,,我在function.php中根本没有找到怎么办?
    源代码:
    <?php
    /**
    * Twenty Fourteen functions and definitions
    *
    * Set up the theme and provides some helper functions, which are used in the
    * theme as custom template tags. Others are attached to action and filter
    * hooks in WordPress to change core functionality.
    *
    * When using a child theme you can override certain functions (those wrapped
    * in a function_exists() call) by defining them first in your child theme's
    * functions.php file. The child theme's functions.php file is included before
    * the parent theme's file, so the child theme functions would be used.
    *
    * @link https://codex.wordpress.org/Theme_Development
    * @link https://codex.wordpress.org/Child_Themes
    *
    * Functions that are not pluggable (not wrapped in function_exists()) are
    * instead attached to a filter or action hook.
    *
    * For more information on hooks, actions, and filters,
    * @link https://codex.wordpress.org/Plugin_API
    *
    * @package WordPress
    * @subpackage Twenty_Fourteen
    * @since Twenty Fourteen 1.0
    */

    /**
    * Set up the content width value based on the theme's design.
    *
    * @see twentyfourteen_content_width()
    *
    * @since Twenty Fourteen 1.0
    */
    if ( ! isset( $content_width ) ) {
    $content_width = 474;
    }

    /**
    * Twenty Fourteen only works in WordPress 3.6 or later.
    */
    if ( version_compare( $GLOBALS['wp_version'], '3.6', '<' ) ) {
    require get_template_directory() . '/inc/back-compat.php';
    }

    if ( ! function_exists( 'twentyfourteen_setup' ) ) :
    /**
    * Twenty Fourteen setup.
    *
    * Set up theme defaults and registers support for various WordPress features.
    *
    * Note that this function is hooked into the after_setup_theme hook, which
    * runs before the init hook. The init hook is too late for some features, such
    * as indicating support post thumbnails.
    *
    * @since Twenty Fourteen 1.0
    */
    function twentyfourteen_setup() {

    /*
    * Make Twenty Fourteen available for translation.
    *
    * Translations can be added to the /languages/ directory.
    * If you're building a theme based on Twenty Fourteen, use a find and
    * replace to change 'twentyfourteen' to the name of your theme in all
    * template files.
    */
    load_theme_textdomain( 'twentyfourteen', get_template_directory() . '/languages' );

    // This theme styles the visual editor to resemble the theme style.
    add_editor_style( array( 'css/editor-style.css', twentyfourteen_font_url(), 'genericons/genericons.css' ) );

    // Add RSS feed links to for posts and comments.
    add_theme_support( ‘automatic-feed-links’ );

    // Enable support for Post Thumbnails, and declare two sizes.
    add_theme_support( ‘post-thumbnails’ );
    set_post_thumbnail_size( 672, 372, true );
    add_image_size( ‘twentyfourteen-full-width’, 1038, 576, true );

    // This theme uses wp_nav_menu() in two locations.
    register_nav_menus( array(
    ‘primary’ => __( ‘Top primary menu’, ‘twentyfourteen’ ),
    ‘secondary’ => __( ‘Secondary menu in left sidebar’, ‘twentyfourteen’ ),
    ) );

    /*
    * Switch default core markup for search form, comment form, and comments
    * to output valid HTML5.
    */
    add_theme_support( ‘html5’, array(
    ‘search-form’, ‘comment-form’, ‘comment-list’, ‘gallery’, ‘caption’
    ) );

    /*
    * Enable support for Post Formats.
    * See https://codex.wordpress.org/Post_Formats
    */
    add_theme_support( ‘post-formats’, array(
    ‘aside’, ‘image’, ‘video’, ‘audio’, ‘quote’, ‘link’, ‘gallery’,
    ) );

    // This theme allows users to set a custom background.
    add_theme_support( ‘custom-background’, apply_filters( ‘twentyfourteen_custom_background_args’, array(
    ‘default-color’ => ‘f5f5f5’,
    ) ) );

    // Add support for featured content.
    add_theme_support( ‘featured-content’, array(
    ‘featured_content_filter’ => ‘twentyfourteen_get_featured_posts’,
    ‘max_posts’ => 6,
    ) );

    // This theme uses its own gallery styles.
    add_filter( ‘use_default_gallery_style’, ‘__return_false’ );

    // Indicate widget sidebars can use selective refresh in the Customizer.
    add_theme_support( ‘customize-selective-refresh-widgets’ );
    }
    endif; // twentyfourteen_setup
    add_action( ‘after_setup_theme’, ‘twentyfourteen_setup’ );

    /**
    * Adjust content_width value for image attachment template.
    *
    * @since Twenty Fourteen 1.0
    */
    function twentyfourteen_content_width() {
    if ( is_attachment() && wp_attachment_is_image() ) {
    $GLOBALS[‘content_width’] = 810;
    }
    }
    add_action( ‘template_redirect’, ‘twentyfourteen_content_width’ );

    /**
    * Getter function for Featured Content Plugin.
    *
    * @since Twenty Fourteen 1.0
    *
    * @return array An array of WP_Post objects.
    */
    function twentyfourteen_get_featured_posts() {
    /**
    * Filter the featured posts to return in Twenty Fourteen.
    *
    * @since Twenty Fourteen 1.0
    *
    * @param array|bool $posts Array of featured posts, otherwise false.
    */
    return apply_filters( ‘twentyfourteen_get_featured_posts’, array() );
    }

    /**
    * A helper conditional function that returns a boolean value.
    *
    * @since Twenty Fourteen 1.0
    *
    * @return bool Whether there are featured posts.
    */
    function twentyfourteen_has_featured_posts() {
    return ! is_paged() && (bool) twentyfourteen_get_featured_posts();
    }

    /**
    * Register three Twenty Fourteen widget areas.
    *
    * @since Twenty Fourteen 1.0
    */
    function twentyfourteen_widgets_init() {
    require get_template_directory() . ‘/inc/widgets.php’;
    register_widget( ‘Twenty_Fourteen_Ephemera_Widget’ );

    register_sidebar( array(
    ‘name’ => __( ‘Primary Sidebar’, ‘twentyfourteen’ ),
    ‘id’ => ‘sidebar-1’,
    ‘description’ => __( ‘Main sidebar that appears on the left.’, ‘twentyfourteen’ ),
    ‘before_widget’ => ”,
    ‘after_widget’ => ”,
    ‘before_title’ => ”,
    ‘after_title’ => ”,
    ) );
    register_sidebar( array(
    ‘name’ => __( ‘Content Sidebar’, ‘twentyfourteen’ ),
    ‘id’ => ‘sidebar-2’,
    ‘description’ => __( ‘Additional sidebar that appears on the right.’, ‘twentyfourteen’ ),
    ‘before_widget’ => ”,
    ‘after_widget’ => ”,
    ‘before_title’ => ”,
    ‘after_title’ => ”,
    ) );
    register_sidebar( array(
    ‘name’ => __( ‘Footer Widget Area’, ‘twentyfourteen’ ),
    ‘id’ => ‘sidebar-3’,
    ‘description’ => __( ‘Appears in the footer section of the site.’, ‘twentyfourteen’ ),
    ‘before_widget’ => ”,
    ‘after_widget’ => ”,
    ‘before_title’ => ”,
    ‘after_title’ => ”,
    ) );
    }
    add_action( ‘widgets_init’, ‘twentyfourteen_widgets_init’ );

    /**
    * Register Lato Google font for Twenty Fourteen.
    *
    * @since Twenty Fourteen 1.0
    *
    * @return string
    */
    function twentyfourteen_font_url() {
    $font_url = ”;
    /*
    * Translators: If there are characters in your language that are not supported
    * by Lato, translate this to ‘off’. Do not translate into your own language.
    */
    if ( ‘off’ !== _x( ‘on’, ‘Lato font: on or off’, ‘twentyfourteen’ ) ) {
    $query_args = array(
    ‘family’ => urlencode( ‘Lato:300,400,700,900,300italic,400italic,700italic’ ),
    ‘subset’ => urlencode( ‘latin,latin-ext’ ),
    );
    $font_url = add_query_arg( $query_args, ‘https://fonts.googleapis.com/css’ );
    }

    return $font_url;
    }

    /**
    * Enqueue scripts and styles for the front end.
    *
    * @since Twenty Fourteen 1.0
    */
    function twentyfourteen_scripts() {
    // Add Lato font, used in the main stylesheet.
    wp_enqueue_style( ‘twentyfourteen-lato’, twentyfourteen_font_url(), array(), null );

    // Add Genericons font, used in the main stylesheet.
    wp_enqueue_style( ‘genericons’, get_template_directory_uri() . ‘/genericons/genericons.css’, array(), ‘3.0.3’ );

    // Load our main stylesheet.
    wp_enqueue_style( ‘twentyfourteen-style’, get_stylesheet_uri() );

    // Load the Internet Explorer specific stylesheet.
    wp_enqueue_style( ‘twentyfourteen-ie’, get_template_directory_uri() . ‘/css/ie.css’, array( ‘twentyfourteen-style’ ), ‘20131205’ );
    wp_style_add_data( ‘twentyfourteen-ie’, ‘conditional’, ‘lt IE 9’ );

    if ( is_singular() && comments_open() && get_option( ‘thread_comments’ ) ) {
    wp_enqueue_script( ‘comment-reply’ );
    }

    if ( is_singular() && wp_attachment_is_image() ) {
    wp_enqueue_script( ‘twentyfourteen-keyboard-image-navigation’, get_template_directory_uri() . ‘/js/keyboard-image-navigation.js’, array( ‘jquery’ ), ‘20130402’ );
    }

    if ( is_active_sidebar( ‘sidebar-3’ ) ) {
    wp_enqueue_script( ‘jquery-masonry’ );
    }

    if ( is_front_page() && ‘slider’ == get_theme_mod( ‘featured_content_layout’ ) ) {
    wp_enqueue_script( ‘twentyfourteen-slider’, get_template_directory_uri() . ‘/js/slider.js’, array( ‘jquery’ ), ‘20131205’, true );
    wp_localize_script( ‘twentyfourteen-slider’, ‘featuredSliderDefaults’, array(
    ‘prevText’ => __( ‘Previous’, ‘twentyfourteen’ ),
    ‘nextText’ => __( ‘Next’, ‘twentyfourteen’ )
    ) );
    }

    wp_enqueue_script( ‘twentyfourteen-script’, get_template_directory_uri() . ‘/js/functions.js’, array( ‘jquery’ ), ‘20150315’, true );
    }
    add_action( ‘wp_enqueue_scripts’, ‘twentyfourteen_scripts’ );

    /**
    * Enqueue Google fonts style to admin screen for custom header display.
    *
    * @since Twenty Fourteen 1.0
    */
    function twentyfourteen_admin_fonts() {
    wp_enqueue_style( ‘twentyfourteen-lato’, twentyfourteen_font_url(), array(), null );
    }
    add_action( ‘admin_print_scripts-appearance_page_custom-header’, ‘twentyfourteen_admin_fonts’ );

    if ( ! function_exists( ‘twentyfourteen_the_attached_image’ ) ) :
    /**
    * Print the attached image with a link to the next attached image.
    *
    * @since Twenty Fourteen 1.0
    */
    function twentyfourteen_the_attached_image() {
    $post = get_post();
    /**
    * Filter the default Twenty Fourteen attachment size.
    *
    * @since Twenty Fourteen 1.0
    *
    * @param array $dimensions {
    * An array of height and width dimensions.
    *
    * @type int $height Height of the image in pixels. Default 810.
    * @type int $width Width of the image in pixels. Default 810.
    * }
    */
    $attachment_size = apply_filters( ‘twentyfourteen_attachment_size’, array( 810, 810 ) );
    $next_attachment_url = wp_get_attachment_url();

    /*
    * Grab the IDs of all the image attachments in a gallery so we can get the URL
    * of the next adjacent image in a gallery, or the first image (if we’re
    * looking at the last image in a gallery), or, in a gallery of one, just the
    * link to that image file.
    */
    $attachment_ids = get_posts( array(
    ‘post_parent’ => $post->post_parent,
    ‘fields’ => ‘ids’,
    ‘numberposts’ => -1,
    ‘post_status’ => ‘inherit’,
    ‘post_type’ => ‘attachment’,
    ‘post_mime_type’ => ‘image’,
    ‘order’ => ‘ASC’,
    ‘orderby’ => ‘menu_order ID’,
    ) );

    // If there is more than 1 attachment in a gallery…
    if ( count( $attachment_ids ) > 1 ) {
    foreach ( $attachment_ids as $attachment_id ) {
    if ( $attachment_id == $post->ID ) {
    $next_id = current( $attachment_ids );
    break;
    }
    }

    // get the URL of the next image attachment…
    if ( $next_id ) {
    $next_attachment_url = get_attachment_link( $next_id );
    }

    // or get the URL of the first image attachment.
    else {
    $next_attachment_url = get_attachment_link( reset( $attachment_ids ) );
    }
    }

    printf( ‘%2$s‘,
    esc_url( $next_attachment_url ),
    wp_get_attachment_image( $post->ID, $attachment_size )
    );
    }
    endif;

    if ( ! function_exists( ‘twentyfourteen_list_authors’ ) ) :
    /**
    * Print a list of all site contributors who published at least one post.
    *
    * @since Twenty Fourteen 1.0
    */
    function twentyfourteen_list_authors() {
    $contributor_ids = get_users( array(
    ‘fields’ => ‘ID’,
    ‘orderby’ => ‘post_count’,
    ‘order’ => ‘DESC’,
    ‘who’ => ‘authors’,
    ) );

    foreach ( $contributor_ids as $contributor_id ) :
    $post_count = count_user_posts( $contributor_id );

    // Move on if user has not published a post (yet).
    if ( ! $post_count ) {
    continue;
    }
    ?>

    <a class="button contributor-posts-link" href="”>



    = 2 || $page >= 2 ) && ! is_404() ) {
    $title = “$title $sep ” . sprintf( __( ‘Page %s’, ‘twentyfourteen’ ), max( $paged, $page ) );
    }

    return $title;
    }
    add_filter( ‘wp_title’, ‘twentyfourteen_wp_title’, 10, 2 );

    // Implement Custom Header features.
    require get_template_directory() . ‘/inc/custom-header.php’;

    // Custom template tags for this theme.
    require get_template_directory() . ‘/inc/template-tags.php’;

    // Add Customizer functionality.
    require get_template_directory() . ‘/inc/customizer.php’;

    /*
    * Add Featured Content functionality.
    *
    * To overwrite in a plugin, define your own Featured_Content class on or
    * before the ‘setup_theme’ hook.
    */
    if ( ! class_exists( ‘Featured_Content’ ) && ‘plugins.php’ !== $GLOBALS[‘pagenow’] ) {
    require get_template_directory() . ‘/inc/featured-content.php’;
    }

    2016-6-7
  4. 超級efly :

    前來支持一下~拜託加一個本站的參考鏈接啦~

    2014-9-26
    • 远方的海水神 :

      好叻,其实代码是在别处百度到的,然后有点小错,我还修改了下~

      2014-9-26