Change Comment Text

时间:2016-12-07 作者:James Winfield

是否可以更改注释元字段?

我尝试搜索文件结构,但看不到编写它的函数-我只想更改单词“says”

<ol class="comment-list">
            <li id="comment-2" class="comment byuser comment-author-james bypostauthor even thread-even depth-1 parent">
    <article id="div-comment-2" class="comment-body">
        <footer class="comment-meta">
            <div class="comment-author vcard">
                                        <b class="fn">james</b> <span class="says">says:</span>                 </div><!-- .comment-author -->

            <div class="comment-metadata">
谢谢詹姆斯

2 个回复
SO网友:prosti

在你的主题中需要找到wp_list_comments 它接受一些论点。其中一个论点是format. 默认格式为html5, 但还有一个/wp-includes/class-walker-comment.php 调用xhtml.

以下是wp_list_comments:

* @param string|array $args {
 *     Optional. Formatting options.
 *
 *     @type object $walker            Instance of a Walker class to list comments. Default null.
 *     @type int    $max_depth         The maximum comments depth. Default empty.
 *     @type string $style             The style of list ordering. Default \'ul\'. Accepts \'ul\', \'ol\'.
 *     @type string $callback          Callback function to use. Default null.
 *     @type string $end-callback      Callback function to use at the end. Default null.
 *     @type string $type              Type of comments to list.
 *                                     Default \'all\'. Accepts \'all\', \'comment\', \'pingback\', \'trackback\', \'pings\'.
 *     @type int    $page              Page ID to list comments for. Default empty.
 *     @type int    $per_page          Number of comments to list per page. Default empty.
 *     @type int    $avatar_size       Height and width dimensions of the avatar size. Default 32.
 *     @type string $reverse_top_level Ordering of the listed comments. Default null. Accepts \'desc\', \'asc\'.
 *     @type bool   $reverse_children  Whether to reverse child comments in the list. Default null.
 *     @type string $format            How to format the comments list.
 *                                     Default \'html5\' if the theme supports it. Accepts \'html5\', \'xhtml\'.
 *     @type bool   $short_ping        Whether to output short pings. Default false.
 *     @type bool   $echo              Whether to echo the output or return it. Default true.
 * }
 * @param array $comments Optional. Array of WP_Comment objects.
 */
function wp_list_comments( $args = array(), $comments = null ) {
您可以定义格式。

wp_list_comments( ... , \'myformat\', ...);

// create a callback
if( function_exists( \'myformat_comment\' ) )
{
    $args[\'format\'] = \'myformat\';
    $args[\'callback\'] = \'myformat_comment\';
}
在新功能中。

function myformat_comment( $comment, $depth, $args ){}
依据:

protected function html5_comment( $comment, $depth, $args ) {
        $tag = ( \'div\' === $args[\'style\'] ) ? \'div\' : \'li\';
?>
        <<?php echo $tag; ?> id="comment-<?php comment_ID(); ?>" <?php comment_class( $this->has_children ? \'parent\' : \'\', $comment ); ?>>
            <article id="div-comment-<?php comment_ID(); ?>" class="comment-body">
                <footer class="comment-meta">
                    <div class="comment-author vcard">
                        <?php if ( 0 != $args[\'avatar_size\'] ) echo get_avatar( $comment, $args[\'avatar_size\'] ); ?>
                        <?php printf( __( \'%s <span class="says">says:</span>\' ), sprintf( \'<b class="fn">%s</b>\', get_comment_author_link( $comment ) ) ); ?>
                    </div><!-- .comment-author -->

                    <div class="comment-metadata">
                        <a href="<?php echo esc_url( get_comment_link( $comment, $args ) ); ?>">
                            <time datetime="<?php comment_time( \'c\' ); ?>">
                                <?php
                                    /* translators: 1: comment date, 2: comment time */
                                    printf( __( \'%1$s at %2$s\' ), get_comment_date( \'\', $comment ), get_comment_time() );
                                ?>
                            </time>
                        </a>
                        <?php edit_comment_link( __( \'Edit\' ), \'<span class="edit-link">\', \'</span>\' ); ?>
                    </div><!-- .comment-metadata -->

                    <?php if ( \'0\' == $comment->comment_approved ) : ?>
                    <p class="comment-awaiting-moderation"><?php _e( \'Your comment is awaiting moderation.\' ); ?></p>
                    <?php endif; ?>
                </footer><!-- .comment-meta -->

                <div class="comment-content">
                    <?php comment_text(); ?>
                </div><!-- .comment-content -->

                <?php
                comment_reply_link( array_merge( $args, array(
                    \'add_below\' => \'div-comment\',
                    \'depth\'     => $depth,
                    \'max_depth\' => $args[\'max_depth\'],
                    \'before\'    => \'<div class="reply">\',
                    \'after\'     => \'</div>\'
                ) ) );
                ?>
            </article><!-- .comment-body -->
<?php
    }

SO网友:CK MacLeod

达到预期效果的一个简单方法是过滤get_comment_author_link 并将“says”类设置为不显示在样式表中:

CSS:

.says {
    display: none ; 
}
上述内容似乎有点强迫或不雅观,但很简单,基本上就是WordPress年度主题不喜欢说“说”的方式,尽管他们可能会使用更复杂的“腰带+吊带”方式。“2015”淘汰赛是这样说的:

.says, .screen-reader-text {
    clip: rect(1px, 1px, 1px, 1px);
    height: 1px;
    overflow: hidden;
    position: absolute !important;
    width: 1px;
}
不管怎样,既然你不再说says了,你可以说些别的:

功能。php或插件:

add_filter( \'get_comment_author_link\' , \'wpse_better_than_says\' ) ;

function wpse_better_than_says( $link ) {

    //avoid pesky warning notices by declaring variable
    $better = \'\' ;

    //use in comment threads (not widgets and things) only: 
    if ( is_singular() && in_the_loop() ) {

        $better = \'<span class="opines">&nbsp;opines herewith:</span>\' ;

    }

    return $link . $better ; 

}