
Отворете functions.php на Вашата тема и поставете следния код:
require get_template_directory() . '/widgets/agenda.php';
agenda.php
<?php
class agenda_widget extends WP_Widget {
function agenda_widget() {
parent::WP_Widget(false, $name = 'Get posts from different post types');
}
function widget($args, $instance){
extract( $args );
$title = apply_filters('widget_title', $instance['title']);
$posttype = $instance['posttype'] ? $instance['posttype'] : 'post';
$orderby = $instance['orderby'] ? $instance['orderby'] : 'ID';
$orderbyad = $instance['orderbyad'] ? $instance['orderbyad'] : 'ASC';
$limit = $instance['limit'] ? $instance['limit'] : 5;
$bottom = $instance['bottom'];
$args = array( 'posts_per_page' => $limit, 'orderby' => $orderby, 'order'=>$orderbyad, 'post_type' => $posttype);
query_posts($args);
$trainingen = get_posts( $args );
?>
<?php echo $before_widget; ?>
<div class="widget-agenda">
<h3><?php echo $title ?></h3>
<ul>
<?php foreach ($trainingen as $training): ?>
<?php if($training->dates) $dates = json_decode($training->dates) ?>
<li><a href="<?php echo get_permalink($training->ID)?>"><?php //echo date_i18n(get_option("date_format"), strtotime($dates[0])) ?><?php _e($training->post_title); ?></a></li>
<?php endforeach; ?>
</ul>
</div>
<?php echo $after_widget; ?>
<?php
}
function update($new_instance, $old_instance){
$instance = $old_instance;
$instance['title'] = strip_tags($new_instance['title']);
$instance['posttype'] = strip_tags($new_instance['posttype']);
$instance['orderby'] = strip_tags($new_instance['orderby']);
$instance['orderbyad'] = strip_tags($new_instance['orderbyad']);
$instance['limit'] = strip_tags($new_instance['limit']);
return $instance;
}
function form($instance){
$title = esc_attr($instance['title']);
$posttype = esc_attr($instance['posttype']);
$orderby = esc_attr($instance['orderby']);
$orderbyad = esc_attr($instance['orderbyad']);
$limit = esc_attr($instance['limit']);
?>
<p>
<label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:'); ?></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 $title; ?>" />
</p>
<p>
<label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Select Post Type:'); ?></label>
<?
$args = array(
'public' => true,
'_builtin' => false
);
$output = 'names'; // names or objects, note names is the default
$operator = 'and'; // 'and' or 'or'
$post_types = get_post_types( $args, $output, $operator );
?>
<select id="<?php echo $this->get_field_id('posttype'); ?>" name="<?php echo $this->get_field_name('posttype'); ?>">
<option value="post" <? if ($posttype == 'post'){ ?>selected<? } ?>>post</option>
<?
foreach ( $post_types as $post_type )
{
?>
<option value="<?=$post_type;?>" <? if ($posttype == $post_type){ ?>selected<? } ?>><?=$post_type;?></option>
<?
}
?>
</select></br>
<label for="<?php echo $this->get_field_id('orderby'); ?>"><?php _e('Order by'); ?></label>: <select id="<?php echo $this->get_field_id('orderby'); ?>" name="<?php echo $this->get_field_name('orderby'); ?>">
<option value="ID" <? if ($orderby == 'ID'){ ?>selected<? } ?>>ID</option>
</select></br>
<label for="<?php echo $this->get_field_id('orderbyad'); ?>"><?php _e('Order'); ?></label>: <select id="<?php echo $this->get_field_id('orderbyad'); ?>" name="<?php echo $this->get_field_name('orderbyad'); ?>">
<option value="ASC" <? if ($orderbyad == 'ASC'){ ?>selected<? } ?>>ASC</option>
<option value="DESC" <? if ($orderbyad == 'DESC'){ ?>selected<? } ?>>DESC</option>
</select>
</p>
<p>
<label for="<?php echo $this->get_field_id('limit'); ?>"><?php _e('Limit'); ?></label>
<input class="tiny-text" id="<?php echo $this->get_field_id('limit'); ?>" name="<?php echo $this->get_field_name('limit'); ?>" type="number" value="<?php echo $limit; ?>" />
</p>
<?php
}
}
add_action('widgets_init', create_function('', 'return register_widget("agenda_widget");'));
?>
