Getting commenter meta

时间:2013-12-07 作者:localhost

我想找出评论者的性别,并在此基础上,根据他们的性别,为没有任何图片的评论者分配一张显示图片,即如果男性评论者没有图片,则应为其分配一个男性化身。我有。我找不到询问性别的方法。我怎么能做出那样的东西?我应该把它挂在哪里?

2 个回复
SO网友:Chittaranjan

根据上面的评论,您不能直接使用此功能。因此,我们需要按照下面给出的步骤添加此功能。

在评论表单中添加性别字段

// Add the actions to show gender field on comment form
add_action( \'comment_form_logged_in_after\', \'wti_additional_comment_field\' );
add_action( \'comment_form_after_fields\', \'wti_additional_comment_field\' );

function wti_additional_comment_field() {
    echo \'<p class="comment-form-gender">\'.
            \'<label for="gender_male">\' . __( \'Gender\' ) . \'</label>\'.
            \'<input id="gender_male" name="gender" type="radio" value="male" checked="checked" /> \' . __( \'Male\' ) .
            \'<input id="gender_female" name="gender" type="radio" value="female" />\' . __( \'Female\' ) .
        \'</p>\';
}
让我们将性别值保存为注释元数据

// Add the action to save the gender in comment
add_action( \'comment_post\', \'wti_save_comment_meta_data\' );

function wti_save_comment_meta_data( $comment_id ) {
    $gender = wp_filter_nohtml_kses( $_POST[\'gender\'] );
    add_comment_meta( $comment_id, \'gender\', $gender );
}
修改获取头像功能以使用默认图像url

// Add the filter to have custom avatar
add_filter(\'get_avatar\', \'wti_custom_avatar\', 10, 5);

function wti_custom_avatar($avatar, $id_or_email, $size, $default, $alt) {
    global $comment;

    if ( is_object ( $comment ) && !empty ( $comment ) ) {
        // Remove to avoid recursion
        remove_filter( \'get_avatar\', \'wti_custom_avatar\' );

        // Get the comment id and gender for that comment
        $comment_id = get_comment_ID();
        $gender = get_comment_meta( $comment_id, \'gender\', true );

        // Assign the image url as per gender
        if ( $gender == \'female\' ) {
            $default = \'default_female_avatar_url\';
        } else {
            $default = \'default_male_avatar_url\';
        }

        // Get the avatar with default url
        $avatar = get_avatar( $comment, $size, $default );

        // Add the filter again
        add_filter( \'get_avatar\', \'wti_custom_avatar\', 10, 5 );
    }

    return $avatar;
}
Few things to note:
-性别设置为male 默认情况下
-您需要更换default_female_avatar_urldefault_male_avatar_url 你的价值观。

SO网友:henrywright

安装BuddyPress,然后创建xProfile字段,如名称、位置、,gender 等等然后简单地写一个条件语句来查找成员的性别。i、 e伪代码:

if gender == male then do this

结束

相关推荐

WP_Query in functions.php

我有一些代码要转换成函数。它工作得很好,直到我将其包装到所述函数中: $args = array( \'posts_per_page\' => -1, \'post_type\' => \'asset\', \'category_name\' => $cat ); $cat_query = new WP_Query( $args );