Importer des Tweets avec certains hashtags dans Wordpress
-
-
Comment voulez-vous dire "Importer destweets".Voulez-vousjuste unepage quigénère une liste detweets récents avec un certain hashtag,ou voulez-vousmettre lestweets dans labase de données?How do you mean "Import tweets". Do you just want a page that generates a list of recent tweets with a certain hashtag, or do you want to put the tweets into the database?
- 0
- 2011-02-15
- John P Bloch
-
1 réponses
- votes
-
- 2011-02-15
J'ai écrit unefonction de shortcodebasée sur leplugin "Twitter Hash Tag Widget"
copiez simplement cettefonction dans votrefichierthemesfunctions.php
function tweets_by_hashtag_9867($atts, $content = null){ extract(shortcode_atts(array( "hashtag" => 'default_tag', "number" => 5, ), $atts)); $api_url = 'http://search.twitter.com/search.json'; $raw_response = wp_remote_get("$api_url?q=%23$hashtag&rpp=$number"); if ( is_wp_error($raw_response) ) { $output = "<p>Failed to update from Twitter!</p>\n"; $output .= "<!--{$raw_response->errors['http_request_failed'][0]}-->\n"; $output .= get_option('twitter_hash_tag_cache'); } else { if ( function_exists('json_decode') ) { $response = get_object_vars(json_decode($raw_response['body'])); for ( $i=0; $i < count($response['results']); $i++ ) { $response['results'][$i] = get_object_vars($response['results'][$i]); } } else { include(ABSPATH . WPINC . '/js/tinymce/plugins/spellchecker/classes/utils/JSON.php'); $json = new Moxiecode_JSON(); $response = @$json->decode($raw_response['body']); } $output = "<div class='twitter-hash-tag'>\n"; foreach ( $response['results'] as $result ) { $text = $result['text']; $user = $result['from_user']; $image = $result['profile_image_url']; $user_url = "http://twitter.com/$user"; $source_url = "$user_url/status/{$result['id']}"; $text = preg_replace('|(https?://[^\ ]+)|', '<a href="$1">$1</a>', $text); $text = preg_replace('|@(\w+)|', '<a href="http://twitter.com/$1">@$1</a>', $text); $text = preg_replace('|#(\w+)|', '<a href="http://search.twitter.com/search?q=%23$1">#$1</a>', $text); $output .= "<div>"; if ( $images ) $output .= "<a href='$user_url'><img src='$image' alt='$user' /></a>"; $output .= "<a href='$user_url'>$user</a>: $text <a href='$source_url'>»</a></div>\n"; } $output .= "<div class='view-all'><a href='http://search.twitter.com/search?q=%23$hashtag'>" . __('View All') . "</a></div>\n"; $output .= "</div>\n"; } return $output; }
puistransformez-leen un code courten ajoutant la ligne suivante à votrefichierthemesfunctions.php:
add_shortcode("hashtag_tweets", "tweets_by_hashtag_9867");
Ilne vous resteplus qu'à créer unenouvellepage ou un articleet à saisir un shortcodeparexemple:
[hashtag_tweets hashtag="YOUR_TAG" number="NUMBER_OF_TWEETS_TO_GET"]
remplacez YOUR_TAGpar votre hashtaget NUMBER_OF_TWEETS_TO_GETpar lenombre detweets que vous souhaitez recevoir.
J'espère que cela vous aidera.
I wrote a shortcode function based on "Twitter Hash Tag Widget" plugin
just copy this function to your themes functions.php file
function tweets_by_hashtag_9867($atts, $content = null){ extract(shortcode_atts(array( "hashtag" => 'default_tag', "number" => 5, ), $atts)); $api_url = 'http://search.twitter.com/search.json'; $raw_response = wp_remote_get("$api_url?q=%23$hashtag&rpp=$number"); if ( is_wp_error($raw_response) ) { $output = "<p>Failed to update from Twitter!</p>\n"; $output .= "<!--{$raw_response->errors['http_request_failed'][0]}-->\n"; $output .= get_option('twitter_hash_tag_cache'); } else { if ( function_exists('json_decode') ) { $response = get_object_vars(json_decode($raw_response['body'])); for ( $i=0; $i < count($response['results']); $i++ ) { $response['results'][$i] = get_object_vars($response['results'][$i]); } } else { include(ABSPATH . WPINC . '/js/tinymce/plugins/spellchecker/classes/utils/JSON.php'); $json = new Moxiecode_JSON(); $response = @$json->decode($raw_response['body']); } $output = "<div class='twitter-hash-tag'>\n"; foreach ( $response['results'] as $result ) { $text = $result['text']; $user = $result['from_user']; $image = $result['profile_image_url']; $user_url = "http://twitter.com/$user"; $source_url = "$user_url/status/{$result['id']}"; $text = preg_replace('|(https?://[^\ ]+)|', '<a href="$1">$1</a>', $text); $text = preg_replace('|@(\w+)|', '<a href="http://twitter.com/$1">@$1</a>', $text); $text = preg_replace('|#(\w+)|', '<a href="http://search.twitter.com/search?q=%23$1">#$1</a>', $text); $output .= "<div>"; if ( $images ) $output .= "<a href='$user_url'><img src='$image' alt='$user' /></a>"; $output .= "<a href='$user_url'>$user</a>: $text <a href='$source_url'>»</a></div>\n"; } $output .= "<div class='view-all'><a href='http://search.twitter.com/search?q=%23$hashtag'>" . __('View All') . "</a></div>\n"; $output .= "</div>\n"; } return $output; }
then turn it into a short code by adding the next line to your themes functions.php file:
add_shortcode("hashtag_tweets", "tweets_by_hashtag_9867");
Then all you have left to do is create a new,page or post and enter a shortcode for example:
[hashtag_tweets hashtag="YOUR_TAG" number="NUMBER_OF_TWEETS_TO_GET"]
change YOUR_TAG to your hashtag and change NUMBER_OF_TWEETS_TO_GET to the nuber of tweets you want to get.
Hope this helps.
-
est-ilpossible defaire chaque spectacle dans unposte séparé?is is possible to make each show in a separate post ?
- 0
- 2011-02-15
- mireille raad
-
@mireille raad: Oui,c'estpossible,vouspouvez changer lafonctionpour utiliser wp_insert_postet celatranscrira vostweets dans desmessages.@mireille raad: Yes its possible you can change the function to use wp_insert_post and that will fatch your tweets into posts.
- 0
- 2011-02-15
- Bainternet
-
Cela ressemble à ce queje cherchais,mercibeaucoup.Où ajouter le wp_insert_postpourm'assurer que lestweets séparés sont ajoutésen tant quemessages?This looks like what i've been searching for, many thanks. Where do I add the wp_insert_post to ensure the separate tweets are added as posts?
Je cherche unmoyen d'importer destweets dans Wordpress sousforme demessages. Enfait,je souhaite afficher destweets sur un certain sujet sur unepage dans Wordpress. Lapage A contiendrait donc destweets sur le hashtag xet lapage B contiendrait destweets avec le hashtag y.
Ilexiste aumoins unplugin quiimporte lestweets (Tweet-Import),maisilne peutimporter que lestweets d'un certain utilisateur,paspar hashtag.
Y a-t-il unmoyen defaire cela?
Vive!