douliang2087 2016-06-27 13:35
浏览 50
已采纳

使用WP过滤器会使值消失?

I'm using this to display some tweets.

After using the override filters as shown here, it makes the timestamp that naturally occurs to disappear.

The author gives us an example that you can use to override the default markup like so:

add_filter('latest_tweets_render_tweet', function( $html, $date, $link, array $tweet ){
    $pic = $tweet['user']['profile_image_url_https'];
    return '<p class="my-tweet"><img src="'.$pic.'"/>'.$html.'</p><p class="my-date"><a href="'.$link.'">'.$date.'</a></p>';
}, 10, 4 );

Here is my version:

add_filter('latest_tweets_render_tweet', function($html){
return
'<div class="row">
    <div class="small-1 columns twitter-icon-wrap">
      <i class="fa fa-twitter tweet-icon fa-2x fa-pull-left"></i>
    </div>
  <div class="small-11 columns tweet-wrap">'.$html.'</div>
  <p class="tweet-details"><a href="" target="_blank"></a></p>
</div>';
}, 10 , 1 );

If I add $date after the $html variable in the function like:

add_filter('latest_tweets_render_tweet', function($html, $date)

I then get a warning:

Warning: Missing argument 2 for ******\******\Extras\{closure}(), called in /srv/www/*********/current/web/wp/wp-includes/plugin.php on line 235 and defined in /srv/www/*******/current/web/app/themes/*********/lib/extras.php on line 144

line 144 is add_filter('latest_tweets_render_tweet', function($html)

If I subsequently add the $date variable into the returned HTML ignorning the warning, I then get an error:

Notice: Undefined variable: date in.....

Although it's a warning, there is still no date displayed. How can I make the date re-appear?

  • 写回答

1条回答 默认 最新

  • dongliu0823 2016-06-27 13:53
    关注

    The issue is the final argument in add_filter() (1). This parameter is the number of accepted arguments.

    add_filter ( string $tag, callable $function_to_add, int $priority = 10, int $accepted_args = 1 )
    

    Since you want to use 2 arguments, you need to change the final argument of add_filter() to 2. In other words:

    add_filter ( 'latest_tweets_render_tweet', function( $html, $date ) {}, 10, 2 );
    

    It's unclear what exactly you're trying to do with $date...so I'll just give you a generic example:

    add_filter('latest_tweets_render_tweet', function( $html, $date ){
        // $html and $date are now available to you
        return true;  // remember to return something (likely something different than this)
    }, 10, 2 );  // <-- This argument is changed to 2
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 高德地图点聚合中Marker的位置无法实时更新
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办