Domanda

sto avendo una funzione di wordpress per visualizzare le ultime 5 tweets degli utenti, questo tweet possono essere visualizzati in due modi uno è attraverso diretta inserendo l'ID utente in menu di amministrazione, che ho creato, e la seconda è attraverso i campi personalizzati,

Qui voglio evitare di campi personalizzati e voglio iniziare con shortcode, So di essere debole nel breve codice di causa questa è la mia seconda domanda in relazione con esso ma hanno bisogno di breve codice in questa funzione

ecco il codice

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');
È stato utile?

Soluzione

Qui si va:

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;
}

Utilizzo:

[latest_tweets "DarthVader" max=5]   

PS: avete qualche exploit nel codice che avete inviato nella sua interrogazione. Si dovrebbe prendere cura di questo ...

PPS: per le persone che vogliono fare qualcosa di simile:

  • Yahoo Query Language , le richieste dovrebbero essere più veloce teoricamente, e si hanno meno possibilità di essere limitati dal servizio si sta richiedendo i dati da

  • utilizzare il formato JSON per i dati richiesti dall'utente

  • l'uso di WP HTTP API per fare richieste esterne

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a wordpress.stackexchange
scroll top