1

我需要更改此功能wp-includes\comment-template.php

function get_comments_link($post_id = 0) {
    return apply_filters( 'get_comments_link', get_permalink( $post_id ) . '#comments', $post_id );
}

对此

function get_comments_link($post_id = 0) {
    return apply_filters( 'get_comments_link', get_permalink( $post_id ) . '#disqusthread', $post_id );
}

但由于它没有连接到“动作挂钩”,我不知道如何“撤消”或“覆盖”它。

4

1 回答 1

0

嗯,这是一个过滤器钩子。每当您找到do_action()apply_filters()在核心代码中时,您将有机会修改行为或执行自定义代码。

动作钩子允许我们在这do_action一点上运行一些东西,它们不返回任何东西。过滤器钩子允许我们修改输出或正在使用的参数apply_filters(),我们必须在回调函数中返回一些东西。请参阅操作和过滤器不是一回事

挂钩遵循惯例add_HOOK( 'hook-name', 'callback_function', PRIORITY, NUM_PARAMETERS );

在 OP 情况下,它应该像(在functions.php中)一样使用:

add_filter( 'get_comments_link', 'callback_so_18783534', 10, 2 );

function callback_so_18783534( $link, $post_id ) {
    return get_permalink( $post_id ) . '#disqusthread';
}

经过测试,我看到没有评论的帖子没有应用此过滤器(通过comments_link())。它是硬编码的,没有可用的钩子。看起来唯一的解决方案是 jQuery。

于 2013-09-13T10:15:06.420 回答