'widget-oceanwp-recent-posts recent-posts-widget',
'description' => esc_html__( 'Shows a listing of your recent or random posts.', 'ocean-extra' ),
'customize_selective_refresh' => true,
)
);
$this->defaults = array(
'title' => esc_html__( 'Recent Posts', 'ocean-extra' ),
'number' => '3',
'post_type' => 'post',
'taxonomy' => '',
'terms' => '',
'order' => 'DESC',
'orderby' => 'date',
);
}
/**
* Front-end display of widget.
*
* @see WP_Widget::widget()
* @since 1.0.0
*
* @param array $args Widget arguments.
* @param array $instance Saved values from database.
*/
public function widget( $args, $instance ) {
$new_instance = wp_parse_args( $instance, $this->defaults );
$title = ! empty( $new_instance['title'] ) ? $new_instance['title'] : '';
$post_type = ! empty( $new_instance['post_type'] ) ? $new_instance['post_type'] : '';
$taxonomy = ! empty( $new_instance['taxonomy'] ) ? $new_instance['taxonomy'] : '';
$terms = ! empty( $new_instance['terms'] ) ? $new_instance['terms'] : '';
$number = ! empty( $new_instance['number'] ) ? $new_instance['number'] : '';
$order = ! empty( $new_instance['order'] ) ? $new_instance['order'] : '';
$orderby = ! empty( $new_instance['orderby'] ) ? $new_instance['orderby'] : '';
// Apply filters to the title
$title = isset( $instance['title'] ) ? apply_filters( 'widget_title', $instance['title'] ) : '';
// Before widget WP hook
echo $args['before_widget'];
// Show widget title
if ( $title ) {
echo $args['before_title'] . esc_html( $title ) . $args['after_title'];
} ?>
$post_type,
'posts_per_page' => $number,
'no_found_rows' => true,
);
// Order params - needs FALLBACK don't ever edit!
if ( ! empty( $orderby ) ) {
$query_args['order'] = $order;
$query_args['orderby'] = $orderby;
} else {
$query_args['orderby'] = $order; // THIS IS THE FALLBACK
}
// Taxonomy args
if ( ! empty( $taxonomy ) && ! empty( $terms ) ) {
// Sanitize terms and convert to array
$terms = str_replace( ', ', ',', $terms );
$terms = explode( ',', $terms );
// Add to query arg
$query_args['tax_query'] = array(
array(
'taxonomy' => $taxonomy,
'field' => 'slug',
'terms' => $terms,
),
);
}
// Exclude current post
if ( is_singular() ) {
$query_args['post__not_in'] = array( get_the_ID() );
}
// Query posts
$oceanwp_query = new WP_Query( $query_args );
if ( $oceanwp_query->have_posts() ) :
while ( $oceanwp_query->have_posts() ) :
$oceanwp_query->the_post();
?>
-
get_the_title(),
'itemprop' => 'image',
)
);
?>
defaults );
$title = ! empty( $new_instance['title'] ) ? $new_instance['title'] : '';
$post_type = ! empty( $new_instance['post_type'] ) ? $new_instance['post_type'] : '';
$taxonomy = ! empty( $new_instance['taxonomy'] ) ? $new_instance['taxonomy'] : '';
$terms = ! empty( $new_instance['terms'] ) ? $new_instance['terms'] : '';
$number = ! empty( $new_instance['number'] ) ? $new_instance['number'] : '';
$order = ! empty( $new_instance['order'] ) ? $new_instance['order'] : '';
$orderby = ! empty( $new_instance['orderby'] ) ? $new_instance['orderby'] : '';
?>