File: /www/wwwroot/1.scey.cn/wp-content/themes/Leading/template-parts/widget/widgets-post-thumbnail.php
<?php
//date_default_timezone_set("Asia/Shanghai");
class xintheme_post_tools_thumbnail extends WP_Widget {
/**
* Sets up the widgets name etc
*/
public function __construct() {
$widget_ops = array(
'classname' => 'xintheme_post_tools_thumbnail',
'description' => '「XinTheme」主题自带文章小工具,带缩略图。',
);
parent::__construct( 'xintheme_post_tools_thumbnail', '「XinTheme」文章(缩略图)', $widget_ops );
}
/**
* Outputs the content of the widget
*
* @param array $args
* @param array $instance
*/
public function widget( $args, $instance ) {
extract($args);
$number = $instance['num'];
$who = $instance['who'];
$days = $instance['days'];
$cat = $instance['cat'];
$post_id = $instance['post_id'];
$strtotime = strtotime('-'.$days.' days');
$title = apply_filters('widget_name', $instance['title']);
switch ($who) {
case 'new_posts':
$args = array(
'type' => 'post',
'showposts' => $number,
'cat' => $cat,
'ignore_sticky_posts' => 1
);
break;
case 'random_posts':
$args = array(
'type' => 'post',
'showposts' => $number,
'orderby' => 'rand',
'cat' => $cat,
'ignore_sticky_posts' => 1
);
break;
case 'views_posts':
$args = array(
'type' => 'post',
'showposts' => $number,
'orderby' => 'meta_value_num',
'cat' => $cat,
'meta_key' => 'views',
'ignore_sticky_posts' => 1,
'date_query' => array(
array(
'before' => date( 'Y-m-d H:00:00', time() ),
),
array(
'after' => date( 'Y-m-d H:00:00', $strtotime ),
),
),
);
break;
default:
$args = null;
break;
}
if( $args ){
$widget_query = wpjam_query($args);
}
?>
<div class="right_news">
<div class="right_news_title">
<p>
<?php echo $title; ?>
</p>
</div>
<ul class="widget_SpecialCatPosts">
<?php
if( $args ){
if ($widget_query->have_posts()) :
while ($widget_query->have_posts()) : $widget_query->the_post();
?>
<li>
<a href="<?php the_permalink(); ?>">
<img src="<?php echo wpjam_get_post_thumbnail_url($post,array(250,188), $crop=1);?>" alt="<?php the_title(); ?>" title="<?php the_title(); ?>" class="thumb"></a>
<h4><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h4>
</li>
<?php
endwhile;
wp_reset_query();
else :
echo '<p>抱歉,没有找到文章!</p>';
endif;
}else{
echo '<p>抱歉,没有找到文章!</p>';
}
?>
</ul>
</div>
<?php
}
/**
* Outputs the options form on admin
*
* @param array $instance The widget options
*/
public function form( $instance ) {
$instance = wp_parse_args( (array) $instance, array(
'title' => '文章小工具',
'num' => 5,
'days' => 30,
'who' => 'new_posts',
'style' => '1',
)
);
?>
<p>
<label> 标题:
<input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo $instance['title']; ?>" />
</label>
</p>
<p>
<label> 显示数量:
<input class="widefat" id="<?php echo $this->get_field_id('num'); ?>" name="<?php echo $this->get_field_name('num'); ?>" type="number" step="1" min="1" value="<?php echo $instance['num']; ?>" />
</label>
</p>
<p>
<p>
<label> 显示什么:
<select class="widefat xintheme_select_handle" id="<?php echo $this->get_field_id('who'); ?>" name="<?php echo $this->get_field_name('who'); ?>">
<option <?php dahuzi_selected( $instance['who'], 'new_posts' ); ?> value="new_posts">最新文章</option>
<option <?php dahuzi_selected( $instance['who'], 'random_posts' ); ?> value="random_posts">随机文章</option>
<option <?php dahuzi_selected( $instance['who'], 'views_posts' ); ?> value="views_posts">浏览最多</option>
<option <?php dahuzi_selected( $instance['who'], 'post_id' ); ?> value="post_id">指定文章</option>
</select>
</label>
</p>
<?php if( $instance['who'] == 'new_posts' || $instance['who'] == 'random_posts' || $instance['who'] == 'post_id' ){ ?>
<p id="<?php echo $this->get_field_id('who'); ?>-box" style="display: none">
<?php }else{ ?>
<p id="<?php echo $this->get_field_id('who'); ?>-box">
<?php } ?>
<label>
显示
<input class="tiny-text" id="<?php echo $this->get_field_id('days'); ?>" name="<?php echo $this->get_field_name('days'); ?>" type="number" step="1" min="1" value="<?php echo $instance['days']; ?>" style="width: 70px;" />
<span>
<?php
switch ($instance['who']) {
case 'views_posts':
echo '天内浏览最多的文章';
break;
default:
break;
}
?>
</span>
</label>
</p>
<?php if( $instance['who'] == 'post_id' ){ ?>
<p id="<?php echo $this->get_field_id('who'); ?>-box-2">
<?php }else{ ?>
<p id="<?php echo $this->get_field_id('who'); ?>-box-2" style="display: none">
<?php }?>
<label>
指定文章ID:
<input style="width:100%;" id="<?php echo $this->get_field_id('post_id'); ?>" name="<?php echo $this->get_field_name('post_id'); ?>" type="text" value="<?php echo $instance['post_id']; ?>" size="24" />
格式:1,2 表示只显示文章ID为1,2分类的文章,注意:多个ID之间用英文逗号隔开!
</label>
</p>
<?php if( $instance['who'] == 'post_id' ){ ?>
<p id="<?php echo $this->get_field_id('who'); ?>-box-3" style="display: none">
<?php }else{ ?>
<p id="<?php echo $this->get_field_id('who'); ?>-box-3">
<?php }?>
<label>
分类限制:
<input style="width:100%;" id="<?php echo $this->get_field_id('cat'); ?>" name="<?php echo $this->get_field_name('cat'); ?>" type="text" value="<?php echo $instance['cat']; ?>" size="24" />
格式:1,2 表示只显示分类ID为1,2分类的文章,格式:-1,-2 表示排除分类ID为1,2的文章,也可以直接写1或者-1;注意:多个ID之间用英文逗号隔开!
</label>
</p>
<?php
}
}
add_action('widgets_init', function(){register_widget('xintheme_post_tools_thumbnail' );});
function dahuzi_selected( $t, $i ){
if( $t == $i ){
echo 'selected';
}
}
/**
* 加载一段JS
*/
function dahuzi_select_handle() {
?>
<script>
jQuery( document ).ready( function() {
if( jQuery('.xintheme_select_handle').length > 0 ){
jQuery('.xintheme_select_handle').each(function(index, el) {
xintheme_select_handle( jQuery(this).attr('id') );
});
}
});
jQuery(document).on('change', '.xintheme_select_handle', function(event) {
event.preventDefault();
xintheme_select_handle( jQuery(this).attr('id') );
});
function xintheme_select_handle( id ){
var selected = jQuery('#'+id+' option:selected');
if( selected.val() == 'comment_posts' || selected.val() == 'views_posts' ){
jQuery('#'+id+'-box label span').text( ' 天内' + selected.text() + '的文章' );
jQuery('#'+id+'-box').show();
jQuery('#'+id+'-box-2').hide();
jQuery('#'+id+'-box-3').show();
}else if(selected.val() == 'post_id'){
jQuery('#'+id+'-box').hide();
jQuery('#'+id+'-box-2').show();
jQuery('#'+id+'-box-3').hide();
}else{
jQuery('#'+id+'-box').hide();
jQuery('#'+id+'-box-2').hide();
jQuery('#'+id+'-box-3').show();
}
}
</script>
<?php
}
add_action( 'admin_footer', 'dahuzi_select_handle' );