douliang2087
douliang2087
2016-06-27 13:35

使用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 dongliu0823 5年前

    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
    
    点赞 评论 复制链接分享

相关推荐