-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathltp-widget.php
78 lines (63 loc) · 2.26 KB
/
ltp-widget.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
<?php
// widget
class ltpLikeWidget extends WP_Widget {
public function __construct() {
$widget_ops = array(
'classname' => 'ltpLikeWidget',
'description' => 'A widget for like this post plugin. List most liked posts.',
);
parent::__construct('ltpLikeWidget', 'like this post widget', $widget_ops);
}
public function widget($args, $instance) {
echo $args['before_widget'];
if (!empty( $instance['title'])) {
print $args['before_title'] . apply_filters('widget_title', $instance['title']) . $args['after_title'];
}
$posts = get_posts(array(
'posts_per_page' => '10',
'meta_key' => 'ltpLikeCount',
'order' => 'desc',
'orderby' => 'meta_value'
));
if(!empty($posts) && is_array($posts)){
?>
<ul>
<?php foreach ($posts as $post) { ?>
<li><a href="<?php print get_permalink( $post->ID ); ?>">
<?php print $post->post_title; ?> - ♡ <?php print get_post_meta($post->ID, 'ltpLikeCount', true); ?>
</a></li>
<?php } ?>
</ul>
<?php
}else{
echo esc_html__('Empty here!', 'text_domain' );
}
echo $args['after_widget'];
}
public function form($instance) {
$title = ! empty($instance['title']) ? $instance['title'] : esc_html__('Title', 'text_domain');
?>
<p>
<label for="<?php echo esc_attr($this->get_field_id('title')); ?>">
<?php esc_attr_e('Title:', 'text_domain'); ?>
</label>
<input
class="widefat"
id="<?php echo esc_attr($this->get_field_id('title')); ?>"
name="<?php echo esc_attr($this->get_field_name('title')); ?>"
type="text"
value="<?php echo esc_attr($title); ?>">
</p>
<?php
}
public function update($new_instance, $old_instance) {
$instance = array();
$instance['title'] = (!empty($new_instance['title'])) ? strip_tags($new_instance['title']) : '';
return $instance;
}
}
add_action('widgets_init', function(){
register_widget('ltpLikeWidget');
});
// end widget
?>