Skip to content

Instantly share code, notes, and snippets.

Created October 23, 2017 05:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/ddb5386ce5d3255fd23220e0bbfa3b2d to your computer and use it in GitHub Desktop.
Save anonymous/ddb5386ce5d3255fd23220e0bbfa3b2d to your computer and use it in GitHub Desktop.
function mediclean_get_slider_details( $input ) {
$featured_slider_type = mediclean_get_option( 'featured_slider_type' );
$featured_slider_number = mediclean_get_option( 'featured_slider_number' );
$featured_slider_read_more_text = mediclean_get_option( 'featured_slider_read_more_text' );
switch ( $featured_slider_type ) {
case 'demo-slider':
$slides = array();
for( $i = 0; $i <= 1 ; $i++ ) {
$img_arr = array(
0 => get_template_directory_uri() . '/images/slide'. ( $i + 1 ) . '.jpg',
1 => 1420,
2 => 440,
3 => 0,
);
$slides[ $i ]['images'] = $img_arr;
$slides[ $i ]['title'] = __( 'Slide Title ','mediclean' ) . ( $i + 1 );
$slides[ $i ]['url'] = esc_url( home_url( '/' ) );
$slides[ $i ]['excerpt'] = __( 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Etiam quis metus scelerisque, faucibus risus eu, luctus est.', 'mediclean' );
}
if ( ! empty( $slides ) ) {
$input = $slides;
}
break;
case 'featured-page':
$ids = array();
for ( $i = 1; $i <= $featured_slider_number ; $i++ ) {
$id = mediclean_get_option( 'featured_slider_page_' . $i );
if ( ! empty( $id ) ) {
$ids[] = absint( $id );
}
}
// Bail if no valid pages are selected.
if ( empty( $ids ) ) {
return $input;
}
$qargs = array(
'posts_per_page' => esc_attr( $featured_slider_number ),
'no_found_rows' => true,
'orderby' => 'post__in',
'post_type' => 'page',
'post__in' => $ids,
'meta_query' => array(
array( 'key' => '_thumbnail_id' ), // Show only posts with featured images.
),
);
// Fetch posts.
$all_posts = get_posts( $qargs );
$slides = array();
if ( ! empty( $all_posts ) ) {
$cnt = 0;
foreach ( $all_posts as $key => $post ) {
if ( has_post_thumbnail( $post->ID ) ) {
$image_array = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'mediclean-slider' );
$slides[ $cnt ]['images'] = $image_array;
$slides[ $cnt ]['title'] = esc_html( $post->post_title );
$slides[ $cnt ]['url'] = esc_url( get_permalink( $post->ID ) );
$slides[ $cnt ]['excerpt'] = mediclean_the_excerpt( apply_filters( 'mediclean_filter_slider_caption_length', 10 ), $post );
if ( ! empty( $featured_slider_read_more_text ) ) {
$slides[ $cnt ]['primary_button_text'] = esc_attr( $featured_slider_read_more_text );
$slides[ $cnt ]['primary_button_url'] = $slides[ $cnt ]['url'];
}
$cnt++;
}
}
}
if ( ! empty( $slides ) ) {
$input = $slides;
}
break;
case 'featured-post':
$ids = array();
for ( $i = 1; $i <= $featured_slider_number ; $i++ ) {
$id = mediclean_get_option( 'featured_slider_post_' . $i );
if ( ! empty( $id ) ) {
$ids[] = absint( $id );
}
}
// Bail if no valid pages are selected.
if ( empty( $ids ) ) {
return $input;
}
$qargs = array(
'posts_per_page' => esc_attr( $featured_slider_number ),
'no_found_rows' => true,
'orderby' => 'post__in',
'post_type' => 'post',
'post__in' => $ids,
'meta_query' => array(
array( 'key' => '_thumbnail_id' ), // Show only posts with featured images.
),
);
// Fetch posts.
$all_posts = get_posts( $qargs );
$slides = array();
if ( ! empty( $all_posts ) ) {
$cnt = 0;
foreach ( $all_posts as $key => $post ) {
if ( has_post_thumbnail( $post->ID ) ) {
$image_array = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'mediclean-slider' );
$slides[ $cnt ]['images'] = $image_array;
$slides[ $cnt ]['title'] = esc_html( $post->post_title );
$slides[ $cnt ]['url'] = esc_url( get_permalink( $post->ID ) );
$slides[ $cnt ]['excerpt'] = mediclean_the_excerpt( apply_filters( 'mediclean_filter_slider_caption_length', 10 ), $post );
if ( ! empty( $featured_slider_read_more_text ) ) {
$slides[ $cnt ]['primary_button_text'] = esc_attr( $featured_slider_read_more_text );
$slides[ $cnt ]['primary_button_url'] = $slides[ $cnt ]['url'];
}
$cnt++;
}
}
}
if ( ! empty( $slides ) ) {
$input = $slides;
}
break;
case 'featured-category':
$featured_slider_category = mediclean_get_option( 'featured_slider_category' );
$qargs = array(
'posts_per_page' => esc_attr( $featured_slider_number ),
'no_found_rows' => true,
'post_type' => 'post',
'meta_query' => array(
array( 'key' => '_thumbnail_id' ), // Show only posts with featured images.
),
);
if ( absint( $featured_slider_category ) > 0 ) {
$qargs['category'] = esc_attr( $featured_slider_category );
}
// Fetch posts.
$all_posts = get_posts( $qargs );
$slides = array();
if ( ! empty( $all_posts ) ) {
$cnt = 0;
foreach ( $all_posts as $key => $post ) {
if ( has_post_thumbnail( $post->ID ) ) {
$image_array = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'mediclean-slider' );
$slides[ $cnt ]['images'] = $image_array;
$slides[ $cnt ]['title'] = esc_html( $post->post_title );
$slides[ $cnt ]['url'] = esc_url( get_permalink( $post->ID ) );
$slides[ $cnt ]['excerpt'] = mediclean_the_excerpt( apply_filters( 'mediclean_filter_slider_caption_length', 10 ), $post );
if ( ! empty( $featured_slider_read_more_text ) ) {
$slides[ $cnt ]['primary_button_text'] = esc_attr( $featured_slider_read_more_text );
$slides[ $cnt ]['primary_button_url'] = $slides[ $cnt ]['url'];
}
$cnt++;
}
}
}
if ( ! empty( $slides ) ) {
$input = $slides;
}
break;
case 'featured-tag':
$featured_slider_tag = mediclean_get_option( 'featured_slider_tag' );
$qargs = array(
'posts_per_page' => esc_attr( $featured_slider_number ),
'no_found_rows' => true,
'post_type' => 'post',
'meta_query' => array(
array( 'key' => '_thumbnail_id' ), // Show only posts with featured images.
),
);
if ( absint( $featured_slider_tag ) > 0 ) {
$qargs['tag_id'] = esc_attr( $featured_slider_tag );
}
// Fetch posts.
$all_posts = get_posts( $qargs );
$slides = array();
if ( ! empty( $all_posts ) ) {
$cnt = 0;
foreach ( $all_posts as $key => $post ) {
if ( has_post_thumbnail( $post->ID ) ) {
$image_array = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'mediclean-slider' );
$slides[ $cnt ]['images'] = $image_array;
$slides[ $cnt ]['title'] = esc_html( $post->post_title );
$slides[ $cnt ]['url'] = esc_url( get_permalink( $post->ID ) );
$slides[ $cnt ]['excerpt'] = mediclean_the_excerpt( apply_filters( 'mediclean_filter_slider_caption_length', 10 ), $post );
if ( ! empty( $featured_slider_read_more_text ) ) {
$slides[ $cnt ]['primary_button_text'] = esc_attr( $featured_slider_read_more_text );
$slides[ $cnt ]['primary_button_url'] = $slides[ $cnt ]['url'];
}
$cnt++;
}
}
}
if ( ! empty( $slides ) ) {
$input = $slides;
}
break;
case 'featured-image':
$slides = array();
for ( $i = 1; $i <= $featured_slider_number; $i++ ) {
$photo = mediclean_get_option( 'featured_slider_image_photo_' . $i );
if ( ! empty( $photo ) ) {
$item = array();
$item['images'][0] = esc_url( $photo );
$title = mediclean_get_option( 'featured_slider_image_title_' . $i );
$url = mediclean_get_option( 'featured_slider_image_url_' . $i );
$caption = mediclean_get_option( 'featured_slider_image_caption_' . $i );
$new_window = mediclean_get_option( 'featured_slider_image_new_window_' . $i );
$item['title'] = esc_html( $title );
$item['url'] = esc_url( $url );
$item['excerpt'] = esc_html( $caption );
$item['new_window'] = esc_attr( $new_window );
$item['primary_button_text'] = esc_attr( mediclean_get_option( 'featured_slider_image_primary_button_text_' . $i ) );
$item['primary_button_url'] = esc_attr( mediclean_get_option( 'featured_slider_image_primary_button_url_' . $i ) );
$item['secondary_button_text'] = esc_attr( mediclean_get_option( 'featured_slider_image_secondary_button_text_' . $i ) );
$item['secondary_button_url'] = esc_attr( mediclean_get_option( 'featured_slider_image_secondary_button_url_' . $i ) );
$slides[] = $item;
}
}
if ( ! empty( $slides ) ) {
$input = $slides;
}
break;
default:
break;
}
return $input;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment