全部, 我有以下代码来检索一些推文:
if( ! $tweet ) {
$url = "http://api.twitter.com/1/statuses/user_timeline.json?screen_name={$username}&count={$how_many}";
$curl = curl_init();
curl_setopt( $curl, CURLOPT_URL, $url );
curl_setopt( $curl, CURLOPT_RETURNTRANSFER, 1 );
$contents = curl_exec( $curl );
curl_close( $curl );
if ( ! empty( $contents ) ) {
// Decode it.
$tweet = json_decode( $contents );
}
}
// Check to make sure we have a tweet and display it.
if ( $tweet ) {
do_action( 'themeblvd_twitter_slider', $tweet, $options, $username, $slider_id );
} else {
$output = 'Twitter timed out.';
}
return $output;
然后我将以下代码放入Slider:
function themeblvd_twitter_slider_default( $tweet, $options, $username, $slider_id ) {
$tweet = (array)$tweet;
$classes = themeblvd_get_classes( 'slider_standard', true );
$classes .= ' hide-nav_standard';
$classes .= ' show-nav_arrows';
$classes .= ' show-pause_play';
//themeblvd_twitter_slider_js( $slider_id, $options );
?>
<div id="tb-slider-<?php echo $slider_id; ?>" class="slider-wrapper standard-slider-wrapper">
<div class="slider-inner<?php echo $classes; ?>">
<div class="slides-wrapper slides-wrapper-<?php echo $slider_id; ?>">
<div class="slides-inner">
<div class="slider standard-slider flexslider">
<div class="tb-loader"></div>
<ul class="slides">
<?php
foreach($tweet as $t){
?>
<li class="slide tight <?php echo $classes; ?>">
<div class="slide-body">
<div class="grid-protection">
<?php
echo '<span class="tweet-icon '.$options['icon'].'"></span>';
echo '<a href="http://twitter.com/'.$username.'/status/'.$t->id_str.'" target="_blank">';
echo $t->text;
echo '</a>';
?>
</div><!-- .grid-protection (end) -->
</div><!-- .slide-body (end) -->
</li>
<?php
}
?>
</ul>
</div><!-- .slider (end) -->
</div><!-- .slides-inner (end) -->
</div><!-- .slides-wrapper (end) -->
</div><!-- .slider-inner (end) -->
<div class="design-1"></div>
<div class="design-2"></div>
<div class="design-3"></div>
<div class="design-4"></div>
</div><!-- .slider-wrapper (end) -->
<?php
}
?>
此代码大约有90%的时间可以工作,并根据用户名等抓取推文。但是,大约有10%的时间它只是旋转而不显示任何文本。有没有人看到任何东西或方法可以使这个解决方案更好,以便始终显示推文?从错误处理的角度来看,我错过了什么,如果它无法检索推文,我可以添加到不显示它?
感谢您提前获得任何建议。
答案 0 :(得分:0)
您可以尝试检查Twitter实际返回的是推文列表。目前你只检查推特是否会返回任何内容。
// Check to make sure we have a tweet and display it.
if ( is_array($tweet) && isset($tweet[0]->id_str) ) {
do_action( 'themeblvd_twitter_slider', $tweet, $options, $username, $slider_id );
} else {
$output = 'Twitter timed out.';
}