'newsmag_builder',
'description' => __( 'Layout consists of a featured post thumbnail, followed by a handful of posts that are smaller in size. Perfect for emphasising important news.', 'newsmag' ),
'customize_selective_refresh' => true
) );
}
public function enqueue() {
wp_enqueue_script( 'jquery-ui' );
wp_enqueue_script( 'jquery-ui-slider' );
}
public function form( $instance ) {
$defaults = array(
'title' => __( 'Recent posts', 'newsmag' ),
'show_post' => 4,
'newsmag_category' => 'uncategorized',
'featured_article' => 'on',
'show_date' => 'on',
'order' => 'desc'
);
$instance = wp_parse_args( (array) $instance, $defaults );
?>
$args['show_post']
);
/**
* Grab the sticky posts
*/
$sticky_atts = array(
'posts_per_page' => $args['show_post'],
'post__in' => get_option( 'sticky_posts' )
);
$atts['order'] = $args['order'];
$sticky_atts['order'] = $args['order'];
$atts['orderby'] = 'date';
$sticky_atts['orderby'] = 'date';
if('rand' == $atts['order']){
$atts['order'] = '';
$sticky_atts['order'] = '';
$atts['orderby'] = 'rand';
$sticky_atts['orderby'] = 'rand';
}
/**
* Grab category and add the new argument
*/
$idObj = get_category_by_slug( $args['newsmag_category'] );
if ( $idObj ) {
$id = $idObj->term_id;
$atts['cat'] = $id;
$sticky_atts['cat'] = $id;
}
/**
* Initiate WP Query for the sticky posts
*/
$sticky = new WP_Query( $sticky_atts );
wp_reset_postdata();
$sticky_post_ids = array();
/**
* Start adding the IDS of the sticky posts in a new array
*/
if ( ! empty( $sticky->posts ) ) {
foreach ( $sticky->posts as $post ) {
$sticky_post_ids[] = $post->ID;
}
}
/**
* Run the normal query
*/
$normal_posts = new WP_Query( $atts );
wp_reset_postdata();
/**
* In case we do not have sticky posts, we terminate here and return this result
*/
if ( empty( $sticky->posts ) ) {
return $normal_posts;
}
/**
* We check if the post id is in the sticky post id array, and if not - we add it to the sticky posts result
*/
foreach ( $normal_posts->posts as $post ) {
if ( in_array( $post->ID, $sticky_post_ids ) ) {
continue;
}
$sticky->posts[] = $post;
}
$sticky->posts = array_slice( $sticky->posts, 0, (int) $args['show_post'] );
$sticky->post_count = count( $sticky->posts );
return $sticky;
}
public function widget( $args, $instance ) {
$defaults = array(
'title' => __( 'Recent posts', 'newsmag' ),
'show_post' => 4,
'newsmag_category' => '',
'featured_article' => 'on',
'show_date' => 'on',
'order' => 'desc'
);
$instance = wp_parse_args( (array) $instance, $defaults );
echo $args['before_widget'];
$filepath = get_template_directory() . '/inc/libraries/widgets/widget-newsmag-posts-column/layouts/posts_column.php';
if ( $instance['featured_article'] ) {
$filepath = get_template_directory() . '/inc/libraries/widgets/widget-newsmag-posts-column/layouts/posts_column_featured.php';
}
$posts = $this->get_posts( $instance );
if ( file_exists( $filepath ) ) {
include $filepath;
} else {
esc_html_e( 'Please configure your widget', 'newsmag' );
}
echo $args['after_widget'];
}
}