我的主题包括一个显示推文的小部件。我想使用这个小部件并创建自己的版本。我已经成功地在admin中创建了一个新的小部件列表,但在呈现前端时,仍然会调用另一个文件中的小部件函数。我通过在新小部件的widget()函数中放置一个echo来确认这一点。
有关每个文件的代码,请参见以下链接:
Original Theme Widget: 完整代码-http://pastebin.com/asprtK6h
add_action( \'widgets_init\', \'penci_latest_tweets_load_widget\' );
function penci_latest_tweets_load_widget() {
register_widget( \'penci_latest_tweets_widget\' );
}
class penci_latest_tweets_widget extends WP_Widget {
/**
* Widget setup.
*/
function penci_latest_tweets_widget() {
/* Widget settings. */
$widget_ops = array( \'classname\' => \'penci_latest_tweets_widget\', \'description\' => esc_html__(\'A widget that displays your latest tweets with a slider\', \'soledad\') );
/* Widget control settings. */
$control_ops = array( \'width\' => 250, \'height\' => 350, \'id_base\' => \'penci_latest_tweets_widget\' );
/* Create the widget. */
global $wp_version;
if( 4.3 > $wp_version ) {
$this->WP_Widget( \'penci_latest_tweets_widget\', esc_html__(\'.Soledad Tweets Slider\', \'soledad\'), $widget_ops, $control_ops );
} else {
parent::__construct( \'penci_latest_tweets_widget\', esc_html__(\'.Soledad Tweets Slider\', \'soledad\'), $widget_ops, $control_ops );
}
}
/**
* How to display the widget on the screen.
*/
function widget( $args, $instance ) {
extract( $args );
/* Our variables from the widget settings. */
$title = apply_filters( \'widget_title\', $instance[\'title\'] );
$date = isset( $instance[\'date\'] ) ? $instance[\'date\'] : false;
$auto = isset( $instance[\'auto\'] ) ? $instance[\'auto\'] : false;
$reply = isset( $instance[\'reply\'] ) ? $instance[\'reply\'] : esc_html__( \'Reply\', \'soledad\' );
$retweet = isset( $instance[\'retweet\'] ) ? $instance[\'retweet\'] : esc_html__( \'Retweet\', \'soledad\' );
$favorite = isset( $instance[\'favorite\'] ) ? $instance[\'favorite\'] : esc_html__( \'Favorite\', \'soledad\' );
$tweets = getTweets(5);
if( !empty( $tweets ) ):
/* Before widget (defined by themes). */
echo ent2ncr( $before_widget );
/* Display the widget title if one was input (before and after defined by themes). */
if ( $title )
echo ent2ncr( $before_title . $title . $after_title );
if( isset( $tweets[\'error\'] ) ) {
echo \'Missing consumer key - please check your settings in admin > Settings > Twitter Feed Auth\';
} else {
?>
<div class="penci-tweets-widget-content">
<span class="icon-tweets"><i class="fa fa-twitter"></i></span>
<div class="penci-slider penci-tweets-slider" data-smooth="true" data-direction="horizontal" data-auto="<?php if( $auto ){ echo \'false\'; } else { echo \'true\'; } ?>" data-dir="true" data-control="false" data-autotime="5000" data-speed="500">
<ul class="slides">
<?php foreach( $tweets as $tweet ):
$date_array = explode( \' \', $tweet[\'created_at\'] );
$tweet_id = $tweet[\'id_str\'];
$tweet_text = $tweet[\'text\'];
$urls = $tweet[\'entities\'][\'urls\'];
if( isset( $urls ) ) {
foreach ( $urls as $ul ) {
$url = $ul[\'url\'];
if( isset( $url ) ):
$tweet_text = str_replace( $url, \'<a href="\'. $url .\'" target="_blank">\'. $url .\'</a>\', $tweet_text );
endif;
}
}
?>
<li class="penci-tweet">
<div class="tweet-text">
<?php echo $tweet_text; ?>
</div>
<?php if( $date_array[1] && $date_array[2] && $date_array[5] && ! $date ): ?>
<p class="tweet-date"><?php echo $date_array[2] . \'-\' . $date_array[1] . \'-\' . $date_array[5]; ?></p>
<?php endif; ?>
<div class="tweet-intents">
<div class="tweet-intents-inner">
<span><a target="_blank" class="reply" href="https://twitter.com/intent/tweet?in_reply_to=<?php echo sanitize_text_field( $tweet_id ); ?>"><?php echo sanitize_text_field( $reply ); ?></a></span>
<span><a target="_blank" class="retweet" href="https://twitter.com/intent/retweet?tweet_id=<?php echo sanitize_text_field( $tweet_id ); ?>"><?php echo sanitize_text_field( $retweet ); ?></a></span>
<span><a target="_blank" class="favorite" href="https://twitter.com/intent/favorite?tweet_id=<?php echo sanitize_text_field( $tweet_id ); ?>"><?php echo sanitize_text_field( $favorite ); ?></a></span>
</div>
</div>
</li>
<?php endforeach; ?>
</ul>
</div>
</div>
<?php
}
endif; /* End check if array $tweets empty or null */
/* After widget (defined by themes). */
echo ent2ncr( $after_widget );
}
}
New Widget: 完整代码-
http://pastebin.com/wQ839BNF添加\\u操作(\'widgets\\u init\',\'sjcny\\u latest\\u tweets\\u load\\u widget\');
function sjcny_latest_tweets_load_widget() {
register_widget( \'sjcny_latest_tweets_widget\' );
}
class sjcny_latest_tweets_widget extends WP_Widget {
/**
* Widget setup.
*/
function sjcny_latest_tweets_widget() {
/* Widget settings. */
$widget_ops = array( \'classname\' => \'sjcny_latest_tweets_widget\', \'description\' => esc_html__(\'A widget that displays your latest tweets with a slider\', \'soledad\') );
/* Widget control settings. */
$control_ops = array( \'width\' => 250, \'height\' => 350, \'id_base\' => \'sjcny_latest_tweets_widget\' );
/* Create the widget. */
global $wp_version;
if( 4.3 > $wp_version ) {
$this->WP_Widget( \'sjcny_latest_tweets_widget\', esc_html__(\'SJCNY Tweets Slider\', \'soledad\'), $widget_ops, $control_ops );
} else {
parent::__construct( \'sjcny_latest_tweets_widget\', esc_html__(\'SJCNY Tweets Slider\', \'soledad\'), $widget_ops, $control_ops );
}
}
/**
* How to display the widget on the screen.
*/
function widget( $args, $instance ) {
echo "asdf1234";
extract( $args );
/* Our variables from the widget settings. */
$title = apply_filters( \'widget_title\', $instance[\'title\'] );
$date = isset( $instance[\'date\'] ) ? $instance[\'date\'] : false;
$auto = isset( $instance[\'auto\'] ) ? $instance[\'auto\'] : false;
$reply = isset( $instance[\'reply\'] ) ? $instance[\'reply\'] : esc_html__( \'Reply\', \'soledad\' );
$retweet = isset( $instance[\'retweet\'] ) ? $instance[\'retweet\'] : esc_html__( \'Retweet\', \'soledad\' );
$favorite = isset( $instance[\'favorite\'] ) ? $instance[\'favorite\'] : esc_html__( \'Favorite\', \'soledad\' );
$tweets = getTweets(5);
//if( !empty( $tweets ) ):
if( true ) :
/* Before widget (defined by themes). */
echo ent2ncr( $before_widget );
/* Display the widget title if one was input (before and after defined by themes). */
if ( $title )
echo ent2ncr( $before_title . $title . $after_title );
if( isset( $tweets[\'error\'] ) ) {
echo \'Missing consumer key - please check your settings in admin > Settings > Twitter Feed Auth\';
} else {
?>
<div class="penci-tweets-widget-content">
<span class="icon-tweets"><i class="fa fa-twitter"></i></span>
<div class="penci-slider penci-tweets-slider" data-smooth="true" data-direction="horizontal" data-auto="<?php if( $auto ){ echo \'false\'; } else { echo \'true\'; } ?>" data-dir="true" data-control="false" data-autotime="5000" data-speed="500">
<ul class="slides">
<?php foreach( $tweets as $tweet ):
$date_array = explode( \' \', $tweet[\'created_at\'] );
$tweet_id = $tweet[\'id_str\'];
$tweet_text = $tweet[\'text\'];
$urls = $tweet[\'entities\'][\'urls\'];
if( isset( $urls ) ) {
foreach ( $urls as $ul ) {
$url = $ul[\'url\'];
if( isset( $url ) ):
$tweet_text = str_replace( $url, \'<a href="\'. $url .\'" target="_blank">\'. $url .\'</a>\', $tweet_text );
endif;
}
}
?>
<li class="penci-tweet">
<div class="tweet-text">
<?php echo $tweet_text; ?>
</div>
<?php if( $date_array[1] && $date_array[2] && $date_array[5] && ! $date ): ?>
<p class="tweet-date"><?php echo $date_array[2] . \'-\' . $date_array[1] . \'-\' . $date_array[5]; ?></p>
<?php endif; ?>
<div class="tweet-intents">
<div class="tweet-intents-inner">
<!--<span><a target="_blank" class="reply" href="https://twitter.com/intent/tweet?in_reply_to=<?php echo sanitize_text_field( $tweet_id ); ?>"><?php echo sanitize_text_field( $reply ); ?></a></span>-->
<span><a target="_blank" class="reply" href="https://twitter.com/intent/tweet?in_reply_to=<?php echo sanitize_text_field( $tweet_id ); ?>">Justin</a>
<span><a target="_blank" class="retweet" href="https://twitter.com/intent/retweet?tweet_id=<?php echo sanitize_text_field( $tweet_id ); ?>"><?php echo sanitize_text_field( $retweet ); ?></a></span>
<span><a target="_blank" class="favorite" href="https://twitter.com/intent/favorite?tweet_id=<?php echo sanitize_text_field( $tweet_id ); ?>"><?php echo sanitize_text_field( $favorite ); ?></a></span>
</div>
</div>
</li>
<?php endforeach; ?>
</ul>
</div>
</div>
<?php
}
endif; /* End check if array $tweets empty or null */
/* After widget (defined by themes). */
echo ent2ncr( $after_widget );
}
}