如何删除最近评论前的“on”字符串链接?

时间:2016-09-07 作者:Anthony D

我已经知道了如何删除最近评论之前的作者姓名,但不确定在哪里可以找到在最近评论之前添加“on”的代码。

这是我找到的删除作者的代码位:

if ( empty( $url ) || \'http://\' == $url )
        $return = $author;
    else
        // $return = "<a href=\'$url\' rel=\'external nofollow\' class=\'url\'>$author</a>";
如果有人能指出我在哪里可以找到在链接之前删除“on”的代码,我会非常感激,我已经在网站上搜索了一段时间,但没有结果。

1 个回复
最合适的回答,由SO网友:Dave Romsey 整理而成

所讨论的文本来源于wp-includes/widgets/class-wp-widget-recent-comments.php

我通过在WP文件中搜索小部件的id找到了它,recentcomments, 这是我通过检查小部件的HTML收集的。因此,这里是关于的内容的来源,还有一些额外的上下文代码:

foreach ( (array) $comments as $comment ) {
    $output .= \'<li class="recentcomments">\';
    /* translators: comments widget: 1: comment author, 2: post link */
    $output .= sprintf( _x( \'%1$s on %2$s\', \'widgets\' ),
        \'<span class="comment-author-link">\' . get_comment_author_link( $comment ) . \'</span>\',
        \'<a href="\' . esc_url( get_comment_link( $comment ) ) . \'">\' . get_the_title( $comment->comment_post_ID ) . \'</a>\'
    );
    $output .= \'</li>\';
}
在这里我们可以看到$output .= sprintf( _x( \'%1$s on %2$s\', \'widgets\' ), 是我们需要换的绳子。我们可以使用gettext_with_context 筛选以一次性删除作者和单词。

/**
* @param string $translated
* @param string $text
* @param string $context
* @param string $domain
* @return string
*/
function wpse238430_recent_comments_text( $translated, $text, $context, $domain ) {

    // Use guard clauses to bail as soon as possible if we\'re not looking
    // at the things we want. Bailing early is a good practice because 
    // translation filters are called frequently, so code in this function could be run many times, causing an impact on performance.

    // Our string identified above used the \'widgets\' context, so make sure that\'s what we\'re looking at.
    if ( $context !== \'widgets\' ) {
        return $translated;
    }

    // WordPress uses the \'default\' text domain, even though one is not explicitly specified. Bail if the text domain is not \'default\';
    if ( \'default\' !== $domain ) {
        return $translated;
    }   

    // $text contains the string to be evaluated for translation.
    switch ( $text ) {


        case \'%1$s on %2$s\' : // If $text == \'%1$s on %2$s\', do something..
            //$translated = \'%1$s on %2$s\'; // original string. %1$s is the comment author and %2$s is the post they commented on
            $translated = \'%2$s\'; // Remove the author and \'on\'
            break;
    }

    // Return our modified string. Now the original `sprintf()` function will simply output the link to the post that the author commented on.
    return $translated;
}
add_filter( \'gettext_with_context\', \'wpse238430_recent_comments_text\', 20, 4 );