【问题标题】:How to display comment's meta fields value in admin comment section?如何在管理员评论部分显示评论的元字段值?
【发布时间】:2014-06-25 20:09:47
【问题描述】:

我在 wordpress 评论表单中为用户年龄添加了一个额外的字段。 我以这种方式添加了该字段:

add_filter('comment_form_default_fields','add_comment_fields');
function add_comment_fields($fields) {
    $fields['location'] = '<p class="comment-form-location"><label for="location">' . __( Location ) . '</label>' .
        '<input id="location" name="location" type="text" size="30" /></p>';

    return $fields;

}

我还使用“comment_post”操作将元值保存在评论元表中。 现在我必须在管理员评论部分显示这个评论元值。我该怎么做?

【问题讨论】:

    标签: wordpress plugins


    【解决方案1】:

    试试下面的代码,希望你能在管理部分看到你的额外字段(这里我使用年龄作为元键)。

    add_action( 'add_meta_boxes_comment', 'comment_add_meta_box' );
    function comment_add_meta_box()
    {
     add_meta_box( 'my-comment-title', __( 'Your field title' ), 'comment_meta_box_age',     'comment', 'normal', 'high' );
    }
    
    function comment_meta_box_age( $comment )
    {
        $title = get_comment_meta( $comment->comment_ID, 'age', true );
    
       ?>
     <p>
         <label for="age"><?php _e( 'Your label Name' ); ?></label>;
         <input type="text" name="age" value="<?php echo esc_attr( $title ); ?>"  class="widefat" />
     </p>
     <?php
    }
    add_action( 'edit_comment', 'comment_edit_function' );
    function comment_edit_function( $comment_id )
    {
        if( isset( $_POST['age'] ) )
          update_comment_meta( $comment_id, 'age', esc_attr( $_POST['age'] ) );
    }
    

    【讨论】:

      【解决方案2】:
      【解决方案3】:

      如果您成功保存了元值,您可以使用get_comment_meta 函数检索该值。

      模式:

      <?php $meta_values = get_comment_meta( $comment_id, $key, $single ); ?> 
      

      在你的情况下这可能有效:

      <?php $meta_values = get_comment_meta( $comment_id, 'location', true ); ?> 
      

      查看完整文档了解更多详情:http://codex.wordpress.org/Function_Reference/get_comment_meta

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-03-11
        • 1970-01-01
        • 2012-01-26
        • 1970-01-01
        • 2017-07-31
        • 2013-06-05
        • 1970-01-01
        • 2022-11-18
        相关资源
        最近更新 更多