Widget w WordPressie z najpopularniejszymi postami bez użycia plugin’u
Cały kod z pliku najpopularniejsze-posty-widget.php:
<?php
class najpopularniejszePostyWidget extends WP_Widget {
/**
* Sets up the widgets name etc
*/
public function __construct() {
$widget_id = 'najpoularniejszePosty';
$widget_name = __('Widget z najpopularniejszymi postami', 'najpopularniejszePosty');
$widget_opt = array('description' => 'Ten widget wyświetla najpouplarniejsze posty na blogu.');
parent::__construct($widget_id, $widget_name, $widget_opt);
}
/**
* Outputs the content of the widget
*
* @param array $args
* @param array $instance
*/
public function widget( $args, $instance ) {
$title = apply_filters('widget_title', $instance['title']);
if (empty($title))
{
$title = __('Most popular posts', 'najpoularniejszePosty');
}
echo $args['before_widget'];
echo $args['before_title'] . esc_html($title) . $args['after_title'];
$arg = array(
'posts_per_page' => $instance['number'],
'meta_key' => 'post_views_count',
'orderby' => 'meta_value_num'
);
$popular_posts = get_posts($arg);
global $post;
?>
<div class="posts">
<?php foreach($popular_posts as $post) : ?>
<?php setup_postdata( $post ); ?>
<div class="my-articles">
<article>
<?php if ($instance['show_thumbnail'] == TRUE && has_post_thumbnail()): ?>
<div class="post-head">
<?php the_post_thumbnail('medium'); ?>
</div>
<?php endif; ?>
<div>
<h3 class="post-title"><a href="<?php the_permalink(); ?>">
<?php the_title(); ?></a>
<?php if( $instance['show_count'] == TRUE ) : ?>
<span>
<?php
echo "Wyświetlenia: " . get_post_meta($post->ID, 'post_views_count', true);
?>
</span>
<?php
endif; ?>
</h3>
<?php if( $instance['show_excerpt'] == TRUE ) : ?>
<?php echo get_the_excerpt(); ?>
<?php endif; ?>
</div>
</article>
</div>
<?php endforeach; ?>
</div>
<?php
}
/**
* Outputs the options form on admin
*
* @param array $instance The widget options
*/
public function form( $instance ) {
//set default options
$my_defaults = array(
'number' => 5,
'title' => 'Najpouplarniejsze posty',
'show_count' => false, //pokarz liczbę wyświetleń
'show_excerpt' => false, //pokarz początek wpisu
'show_thumbnail' => false //pokarz ikonę wpisu
);
//Refill options stored in the database with the default options.
$instance = wp_parse_args( (array) $instance, $my_defaults );
?>
<p>
<!-- Widgets name !-->
<label for="<?php echo $this->get_field_id('title'); ?>"><?php _e( 'Tytuł:' ); ?></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 esc_attr($instance['title']); ?>" />
</p>
<p>
<!-- Number of posts to show !-->
<label for="<?php echo $this->get_field_id( 'number' ); ?>"><?php _e( 'Liczba wyświetlanych postów:' ); ?></label>
<input id="<?php echo $this->get_field_id( 'number' ); ?>" name="<?php echo $this->get_field_name( 'number' ); ?>" type="text" value="<?php echo $instance['number']; ?>" size="3" />
</p>
<p>
<!-- Show view counts !-->
<input class="checkbox" type="checkbox" name="<?php echo $this->get_field_name('show_count'); ?>" id="<?php echo $this->get_field_id('show_count'); ?>" value="true" <?php checked(true, $instance['show_count']);?> />
<label for="<?php echo $this->get_field_id('show_count'); ?>"> <?php _e( 'Wyświetl liczbę wyświetleń' ); ?></label><br />
</p>
<p>
<!-- Show excerpt !-->
<input class="checkbox" type="checkbox" name="<?php echo $this->get_field_name('show_excerpt'); ?>" id="<?php echo $this->get_field_id('show_excerpt'); ?>" value="true" <?php checked(true, $instance['show_excerpt']);?> />
<label for="<?php echo $this->get_field_id('show_excerpt'); ?>"> <?php _e( 'Wyświetl początek wpisu' ); ?></label><br />
</p>
<p>
<!-- Show thumbnail !-->
<input class="checkbox" type="checkbox" name="<?php echo $this->get_field_name('show_thumbnail'); ?>" id="<?php echo $this->get_field_id('show_thumbnail'); ?>" value="true" <?php checked(true, $instance['show_thumbnail']);?> />
<label for="<?php echo $this->get_field_id('show_thumbnail'); ?>"> <?php _e( 'Wyświetl ikonę wpisu' ); ?></label><br />
</p>
<?php
}
/**
* Processing widget options on save
*
* @param array $new_instance The new options
* @param array $old_instance The previous options
*/
public function update( $new_instance, $old_instance ) {
$instance = $old_instance;
//Widgets name
$instance['title'] = strip_tags($new_instance['title']);
//Number of posts to show
$instance['number'] = absint($new_instance['number']);
//Show view counts
$instance['show_count'] = isset($new_instance['show_count']);
//Show excerpt
$instance['show_excerpt'] = isset($new_instance['show_excerpt']);
//Show thumbnail
$instance['show_thumbnail'] = isset($new_instance['show_thumbnail']);
return $instance;
}
}
?>Przykładowy efekt:
