-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlikes-widget.php
47 lines (40 loc) · 1.28 KB
/
likes-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
<?php
// load widget
function likes_widget_register() {
register_widget( 'like_widget' );
}
add_action( 'widgets_init', 'likes_widget_register' );
// widget settings
class like_widget extends WP_Widget {
function __construct() {
parent::__construct('likes_widget', 'En Çok Beğenilenler', 'En çok beğeni alan yazılar');
}
// widget render
public function widget( $args, $instance ) {
$title = apply_filters( 'widget_title', $instance['title'] );
// before and after widget
echo $args['before_widget'];
if ( ! empty( $title ) )
echo $args['before_title'] . $title . $args['after_title'];
// Likes query
$args = array(
'posts_per_page' => -1,
'meta_key' => 'like_count',
'orderby' => 'meta_value',
'order' => 'DESC',
);
$the_query = new WP_Query($args);
if ( $the_query->have_posts() ) {
echo '<ul>';
while ( $the_query->have_posts() ) {
$the_query->the_post();
$total_like = get_post_meta(get_the_ID(), 'like_total', true);
echo '<li><a href="'. get_the_permalink() .'" title="'.get_the_title().'">' . get_the_title() . '</a> ('. $total_like .')</li>';
}
echo '</ul>';
} else {
echo 'Henüz beğeni alan yazı yok';
}
echo $args['after_widget'];
}
}