Skip to content

Instantly share code, notes, and snippets.

Created Nov 27, 2017
Embed
What would you like to do?
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