Created
March 17, 2017 08:36
-
-
Save qwersk/89e95ae71ea1cd794b4fdc93491c2386 to your computer and use it in GitHub Desktop.
ADD WIDGET WORDPRESS
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Sidebar Categories Widget */ | |
// create category list widget | |
class jpen_Category_List_Widget extends WP_Widget { | |
// php classnames and widget name/description added | |
function __construct() { | |
$widget_options = array( | |
'classname' => 'jpen_category_list_widget', | |
'description' => 'Add latest videos to sidebar' | |
); | |
parent::__construct( | |
'jpen_category_list_widget', | |
'Latest videos in sidebar', | |
$widget_options | |
); | |
} | |
// create the widget output | |
function widget( $args, $instance ) { | |
//$title = apply_filters( 'widget_title', $instance[ 'title' ] ); | |
//echo $args['before_widget'] . $args['before_title'] . $title . $args['after_title']; | |
/* | |
* в массиве задаем все необходимые параметры (более подробно о параметрах чуть ниже) | |
*/ | |
$args = array( | |
'posts_per_page' => 3, | |
'orderby' => 'date', | |
'cat' => 6 | |
); | |
/* | |
* создаем новый объект | |
*/ | |
$q = new WP_Query($args); | |
/* | |
* проверяем, существуют ли посты по заданным параметрам(необязательно) | |
*/ | |
if($q->have_posts()) { | |
echo "<div class='latest_video_widget'>"; | |
/* | |
* затем запускаем цикл | |
*/ | |
while($q->have_posts()){ $q->the_post(); | |
/* | |
* выводим например ссылку на каждый пост | |
*/ | |
$poster = "<img src='".get_field('video_poster')."' />"; | |
echo '<a href="' . get_permalink() . '">' . $poster . '</a>'; | |
} | |
echo "</div>"; | |
} | |
/* | |
* восстанавливаем глобальную переменную $post | |
*/ | |
wp_reset_postdata(); | |
//echo $args['after_widget']; | |
} | |
function form( $instance ) { | |
$title = ! empty( $instance['title'] ) ? $instance['title'] : ''; ?> | |
<!--<p> | |
<label for="<?php echo $this->get_field_id( 'title' ); ?>">Title:</label> | |
<input type="text" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" value="<?php echo esc_attr( $title ); ?>" /> | |
</p>--> | |
<p>This widget displays last 3 videos from «Тренировки» category</p> | |
<?php } | |
// Update database with new info | |
function update( $new_instance, $old_instance ) { | |
$instance = $old_instance; | |
$instance[ 'title' ] = strip_tags( $new_instance[ 'title' ] ); | |
return $instance; | |
} | |
} | |
// register the widget | |
function jpen_register_widgets() { | |
register_widget( 'jpen_Category_List_Widget' ); | |
} | |
add_action( 'widgets_init', 'jpen_register_widgets' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment