0

在以下 WP php 代码中:

function bbp_get_topic_post_count( $topic_id = 0, $integer = false ) {
        $topic_id = bbp_get_topic_id( $topic_id );
        $replies  = (int) get_post_meta( $topic_id, '_bbp_reply_count', true ) + 1;
        $filter   = ( true === $integer ) ? 'bbp_get_topic_post_count_int' : 'bbp_get_topic_post_count';

        return apply_filters( $filter, $replies, $topic_id );
    }

我想通过使用过滤器来更改“$replies”。由于上面有“apply_filters”,我认为可以添加“add_filter”。但似乎过滤器名称是“$filter”

function bbp_reply_count_modified( $replies, $topic_id ) {
            $topic_id = bbp_get_topic_id( $topic_id );
            $replies  = (int) get_post_meta( $topic_id, '_bbp_reply_count', true ); // deleted '+ 1'
            return $replies;
add_filter( '___________________', 'bbp_reply_count_modified', 10, 2 );

在这种情况下,如何创建“add_filter”函数?

谢谢你的帮助。

4

1 回答 1

0

过滤器名称,基于方法的第二个参数 $integer,是bbp_get_topic_post_count_int(when $integeris true) 或bbp_get_topic_post_count(when $integeris false,这是 的默认参数值$integer,如果在方法调用时没有它的值)。

的值在$filter这里分配:

$filter = ( true === $integer ) ? 'bbp_get_topic_post_count_int' : 'bbp_get_topic_post_count';

因此,您不需要修改该方法,而应该搜索该方法的用法以查看 $integer 的哪个输入参数。

要使用$integer = true的过滤器,请使用:

add_filter( 'bbp_get_topic_post_count_int', 'bbp_get_topic_post_count', 10, 2 );

要将过滤器用于$integer = false,请使用:

add_filter( 'bbp_get_topic_post_count', 'bbp_get_topic_post_count', 10, 2 );
于 2015-07-13T21:33:43.227 回答