esc_attr__( 'Random Posts', 'arpw' ), 'title_url' => '', 'offset' => 0, 'limit' => 5, 'orderby' => 'rand', 'post_type' => 'post', 'post_status' => 'publish', 'ignore_sticky' => 1, 'taxonomy' => '', 'cat' => array(), 'tag' => array(), 'thumbnail' => false, 'thumbnail_size' => 'arpw-thumbnail', 'thumbnail_align' => 'left', 'thumbnail_custom' => false, 'thumbnail_width' => '', 'thumbnail_height' => '', 'excerpt' => false, 'excerpt_length' => 10, 'date' => false, 'date_modified' => false, 'date_relative' => false, 'css' => '', 'css_class' => '', 'before' => '', 'after' => '' ); // Allow plugins/themes developer to filter the default arguments. return apply_filters( 'arpw_default_args', $defaults ); } /** * Outputs the random posts. * * @since 0.0.1 */ function arpw_random_posts( $args = array() ) { echo arpw_get_random_posts( $args ); } /** * Generates the random posts markup. * * @since 0.0.1 * @param array $args * @return string|array The HTML for the random posts. */ function arpw_get_random_posts( $args = array() ) { // Set up a default, empty $html variable. $html = ''; // Get the default arguments. $defaults = arpw_get_default_args(); // Merge the input arguments and the defaults. $args = wp_parse_args( $args, $defaults ); // Extract the array to allow easy use of variables. extract( $args ); // Allow devs to hook in stuff before the loop. do_action( 'arpw_before_loop', $args ); // Get the posts query. $posts = arpw_get_posts( $args ); if ( $posts->have_posts() ) : $html = '
'; $html .= ''; $html .= '
'; endif; // Restore original Post Data. wp_reset_postdata(); // Allow devs to hook in stuff after the loop. do_action( 'arpw_after_loop', $args ); // Return the related posts markup. return $args['before'] . $html . $args['after']; } /** * The posts query. * * @since 0.0.1 * @param array $args * @return array */ function arpw_get_posts( $args = array() ) { // Query arguments. $query = array( 'offset' => $args['offset'], 'posts_per_page' => $args['limit'], 'orderby' => $args['orderby'], 'post_type' => $args['post_type'], 'post_status' => $args['post_status'], 'ignore_sticky_posts' => $args['ignore_sticky'], ); // Limit posts based on category. if ( ! empty( $args['cat'] ) ) { $query['category__in'] = $args['cat']; } // Limit posts based on post tag. if ( ! empty( $args['tag'] ) ) { $query['tag__in'] = $args['tag']; } /** * Taxonomy query. * Prop Miniloop plugin by Kailey Lampert. */ if ( ! empty( $args['taxonomy'] ) ) { parse_str( $args['taxonomy'], $taxes ); $tax_query = array(); foreach( array_keys( $taxes ) as $k => $slug ) { $ids = explode( ',', $taxes[ $slug ] ); $tax_query[] = array( 'taxonomy' => $slug, 'field' => 'id', 'terms' => $ids, 'operator' => 'IN' ); } $query['tax_query'] = $tax_query; } // Allow plugins/themes developer to filter the default query. $query = apply_filters( 'arpw_query', $query ); // Perform the query. $posts = new WP_Query( $query ); return $posts; } /** * Custom CSS * * @since 2.0.4 */ function arpw_custom_css( $args ) { if ( ! empty( $args['css'] ) ) { echo ''; } } add_action( 'arpw_before_loop', 'arpw_custom_css', 1 );