Skip to content

Instantly share code, notes, and snippets.

Created November 27, 2017 04:16
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/3af4ad554e13156d498576c4852e311e to your computer and use it in GitHub Desktop.
Save anonymous/3af4ad554e13156d498576c4852e311e to your computer and use it in GitHub Desktop.
function blogism_get_featured_block_details( $input ) {
$featured_block_type = blogism_get_option( 'featured_block_type' );
$featured_block_number = blogism_get_option( 'featured_block_number' );
switch ( $featured_block_type ) {
case 'featured-category':
$featured_block_category = blogism_get_option( 'featured_block_category' );
$qargs = array(
'posts_per_page' => absint( $featured_block_number ),
'no_found_rows' => true,
'post_type' => 'post',
'orderby' => 'rand',
'meta_query' => array(
array( 'key' => '_thumbnail_id' ),
),
);
if ( absint( $featured_block_category ) > 0 ) {
$qargs['category'] = absint( $featured_block_category );
}
// Fetch posts.
$all_posts = get_posts( $qargs );
$items = 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 ), 'blogism-block' );
$items[ $cnt ]['images'] = $image_array;
$items[ $cnt ]['title'] = get_the_title( $post->ID );
$items[ $cnt ]['url'] = get_permalink( $post->ID );
$cnt++;
}
}
}
if ( ! empty( $items ) ) {
$input = $items;
}
break;
case 'featured-tag':
$featured_block_tag = blogism_get_option( 'featured_block_tag' );
$qargs = array(
'posts_per_page' => absint( $featured_block_number ),
'no_found_rows' => true,
'post_type' => 'post',
'meta_query' => array(
array( 'key' => '_thumbnail_id' ),
),
);
if ( absint( $featured_block_tag ) > 0 ) {
$qargs['tag_id'] = absint( $featured_block_tag );
}
// Fetch posts.
$all_posts = get_posts( $qargs );
$items = 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 ), 'blogism-block' );
$items[ $cnt ]['images'] = $image_array;
$items[ $cnt ]['title'] = get_the_title( $post->ID );
$items[ $cnt ]['url'] = get_permalink( $post->ID );
$cnt++;
}
}
}
if ( ! empty( $items ) ) {
$input = $items;
}
break;
case 'sticky-posts':
$sticky = get_option( 'sticky_posts' );
$qargs = array(
'posts_per_page' => absint( $featured_block_number ),
'no_found_rows' => true,
'post_type' => 'post',
'meta_query' => array(
array( 'key' => '_thumbnail_id' ),
),
);
if ( $sticky ) {
$qargs['post__in'] = $sticky;
}
// Fetch posts.
$all_posts = get_posts( $qargs );
$items = 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 ), 'blogism-block' );
$items[ $cnt ]['images'] = $image_array;
$items[ $cnt ]['title'] = get_the_title( $post->ID );
$items[ $cnt ]['url'] = get_permalink( $post->ID );
$cnt++;
}
}
}
if ( ! empty( $items ) ) {
$input = $items;
}
break;
default:
break;
}
return $input;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment