shortcode help require

时间:2010-12-20 作者:Niraj Chauhan

我有一个wordpress功能来显示用户最近的5条推文,这些推文可以通过两种方式显示,一种是通过在我创建的管理菜单中直接输入用户ID,另一种是通过自定义字段,

在这里,我想避免自定义字段,并想从短代码开始,我知道我在短代码方面很弱,因为这是我的第二个与之相关的问题,但这个函数需要短代码

这是密码

function tweet_fetcher(){
global $post;
$doc = new DOMDocument();
if((get_option(\'tweetID\'))!=null){
$meta=get_option(\'tweetID\');
$feed = "http://twitter.com/statuses/user_timeline/$meta.rss"; 
$doc->load($feed); 

}
elseif((get_option(\'tweetID\'))==null){
if ( $meta =get_post_meta($post->ID, \'username:\', true) ) { 
$feed = "http://twitter.com/statuses/user_timeline/$meta.rss"; 
$doc->load($feed); 
}
}

  $outer = "<ul>";
  $max_tweets = 5;   
  $i = 1;
  foreach ($doc->getElementsByTagName(\'item\') as $node) {
    $tweet = $node->getElementsByTagName(\'title\')->item(0)->nodeValue;
    //if you want to remove the userid before the tweets then uncomment the next line.
    //$tweet = substr($tweet, stripos($tweet, \':\') + 1);   
    $tweet = preg_replace(\'@(https?://([-\\w\\.]+)+(:\\d+)?(/([\\w/_\\.]*(\\?\\S+)?)?)?)@\', 
          \'<a href="$1">$1</a>\', $tweet);
    $tweet = preg_replace("/@([0-9a-zA-Z]+)/", 
          "<a href=\\"http://twitter.com/$1\\">@$1</a>", 
          $tweet);

    $outer .= "<li>". $tweet . "</li>\\n";


    if($i++ >= $max_tweets) break;
  }
   $outer .= "</ul>\\n";
  return $outer;
}
function append_the_content($content) {

        $content .="<p>Latest Tweets".tweet_fetcher($meta)."<Script Language=\'Javascript\'>
<!--
document.write(unescape(\'%3C%61%20%68%72%65%66%3D%22%68%74%74%70%3A%2F%2F%6D%62%61%73%2E%69%6E%2F%22%20%74%61%72%67%65%74%3D%22%5F%62%6C%61%6E%6B%22%3E%50%6F%77%65%72%65%64%20%62%79%20%4D%42%41%73%3C%2F%61%3E\'));
//-->
</Script></p>";     
return $content;

        add_filter(\'the_content\', \'append_the_content\');

        require_once (\'tweet-fetcher-admin.php\');

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

给你:

add_shortcode(\'latest_tweets\', \'latest_tweets\');

function latest_tweets($atts){
  extract(shortcode_atts(array(
    \'max\' => 5
  ), $atts));

  $twitter_id = esc_attr(strip_tags($atts[0]));

  // try to get data from cache to avoid slow page loading or twitter blocking
  if (false === ($output = get_transient("latest_tweets_{$twitter_id}"))):

    $doc = new DOMDocument();
    $feed = "http://twitter.com/statuses/user_timeline/{$twitter_id}.rss";
    $doc->load($feed);

    $output = "<ul>";
    $i = 1;
    foreach ($doc->getElementsByTagName(\'item\') as $node) {
      $tweet = $node->getElementsByTagName(\'title\')->item(0)->nodeValue;
      //if you want to remove the userid before the tweets then uncomment the next line.
      //$tweet = substr($tweet, stripos($tweet, \':\') + 1);
      $tweet = preg_replace(\'@(https?://([-\\w\\.]+)+(:\\d+)?(/([\\w/_\\.]*(\\?\\S+)?)?)?)@\', \'<a href="$1">$1</a>\', $tweet);
      $tweet = preg_replace("/@([0-9a-zA-Z]+)/", "<a href=\\"http://twitter.com/$1\\">@$1</a>", $tweet);
      $output .= "<li>{$tweet}</li>\\n";
      if($i++ >= $max) break;
    }
    $output .= "</ul>\\n";
    set_transient("latest_tweets_{$twitter_id}", $output, 60*10); // 10 minute cache
  endif;

  return $output;
}

用法:

[latest_tweets "DarthVader" max=5]   
PS:您在问题中发布的代码中有一些漏洞。你应该注意这个。。。

PPS:对于想做这样事情的人:

使用Yahoo Query Language,从理论上讲,请求应该更快,并且受请求数据的服务的限制也更少

对您请求的数据使用JSON格式

使用WPHTTP API 发出外部请求

结束

相关推荐

Nested Shortcode Detection

如果您熟悉此代码<?php $pattern = get_shortcode_regex(); preg_match(\'/\'.$pattern.\'/s\', $posts[0]->post_content, $matches); if (is_array($matches) && $matches[2] == \'YOURSHORTCODE\') { //shortcode is being used }&#