Skip to main content
replaced http://wordpress.stackexchange.com/ with https://wordpress.stackexchange.com/
Source Link
/**
 * wpse135208_cat_thumb_from_random_child.
 *
 * Get the thumbnail from a random post belonging to one of the child categories.
 *
 * @version 0.1
 *
 * @link httphttps://wordpress.stackexchange.com/q/135208/22534
 *
 * @param integer $c_cat (default: '')
 * @param string/array $size (default: 'post-thumbnail')
 * @param string/array $attr (default: null)
 *
 * @return string
 */
function wpse135208_cat_thumb_from_random_child( $c_cat = '', $size = 'post-thumbnail', $attr = null ) {
    // do nothing if $c_cat is empty
    if( empty($c_cat) ) return;
    // get_terms is used because we only need ids
    $taxonomies = array(
        'category'
        );
    $args = array(
        'child_of' => $c_cat,
        'fields' => 'ids'
        );
    // returns an array of ids
    $child_cats = get_terms( $taxonomies, $args );
    // use this for debugging
    //echo '<pre>'; print_r($child_cats); echo '</pre>';

    $args = array(
        // we use numberposts instead of post_per_page,
        // because if the pre_get_posts filter is used,
        // it can make a difference in this case 
        // we only want one post
        'numberposts' => 1,
        // but we randomize this
        'orderby' => 'rand',
        'category__in' => $child_cats,
        'fields' => 'ids',
        // make sure only posts with featured image are considered
        'meta_query' => array(
            array(
                'key' => '_thumbnail_id',
                'compare' => 'EXISTS'
            )
        )
        );
    // returns an array containing one post id
    $ct_p_id = get_posts( $args );
    // use this for debugging
    //echo '<pre>'; print_r($ct_p_id); echo '</pre>';

    // use this for debugging
    //echo '<pre>'; print_r( ( 1 /*change to 0 to show src info*/ ) ? get_the_post_thumbnail( $ct_p_id[0], $size, $attr ) : wp_get_attachment_image_src( get_post_thumbnail_id( $ct_p_id[0] ) ) ); echo '</pre>'; 
    // now we can use this to return our thumbnail
    return get_the_post_thumbnail( $ct_p_id[0], $size, $attr );
}
/**
 * wpse135208_cat_thumb_wrapper.
 *
 * Wrapper function to have the ability to use predefined image sizes via wp_get_attachment_image().
 *
 * @version 0.1
 *
 * @link httphttps://wordpress.stackexchange.com/q/135208/22534
 *
 * @param integer $category_id (default: null)
 * @param string/array $size (default: 'thumbnail')
 * @param boolean $icon (default: false)
 * @param string/array (default: null)
 *
 * @return string
 */
function wpse135208_cat_thumb_wrapper( $category_id = null, $size = 'thumbnail', $icon = false, $attr = null ) {
    $category_thumbnail_obj = get_the_category_data( $category_id );
    return wp_get_attachment_image( $category_thumbnail_obj->id, $size, $icon, $attr );
}
/**
 * wpse135208_cat_thumb_from_random_child.
 *
 * Get the thumbnail from a random post belonging to one of the child categories.
 *
 * @version 0.1
 *
 * @link http://wordpress.stackexchange.com/q/135208/22534
 *
 * @param integer $c_cat (default: '')
 * @param string/array $size (default: 'post-thumbnail')
 * @param string/array $attr (default: null)
 *
 * @return string
 */
function wpse135208_cat_thumb_from_random_child( $c_cat = '', $size = 'post-thumbnail', $attr = null ) {
    // do nothing if $c_cat is empty
    if( empty($c_cat) ) return;
    // get_terms is used because we only need ids
    $taxonomies = array(
        'category'
        );
    $args = array(
        'child_of' => $c_cat,
        'fields' => 'ids'
        );
    // returns an array of ids
    $child_cats = get_terms( $taxonomies, $args );
    // use this for debugging
    //echo '<pre>'; print_r($child_cats); echo '</pre>';

    $args = array(
        // we use numberposts instead of post_per_page,
        // because if the pre_get_posts filter is used,
        // it can make a difference in this case 
        // we only want one post
        'numberposts' => 1,
        // but we randomize this
        'orderby' => 'rand',
        'category__in' => $child_cats,
        'fields' => 'ids',
        // make sure only posts with featured image are considered
        'meta_query' => array(
            array(
                'key' => '_thumbnail_id',
                'compare' => 'EXISTS'
            )
        )
        );
    // returns an array containing one post id
    $ct_p_id = get_posts( $args );
    // use this for debugging
    //echo '<pre>'; print_r($ct_p_id); echo '</pre>';

    // use this for debugging
    //echo '<pre>'; print_r( ( 1 /*change to 0 to show src info*/ ) ? get_the_post_thumbnail( $ct_p_id[0], $size, $attr ) : wp_get_attachment_image_src( get_post_thumbnail_id( $ct_p_id[0] ) ) ); echo '</pre>'; 
    // now we can use this to return our thumbnail
    return get_the_post_thumbnail( $ct_p_id[0], $size, $attr );
}
/**
 * wpse135208_cat_thumb_wrapper.
 *
 * Wrapper function to have the ability to use predefined image sizes via wp_get_attachment_image().
 *
 * @version 0.1
 *
 * @link http://wordpress.stackexchange.com/q/135208/22534
 *
 * @param integer $category_id (default: null)
 * @param string/array $size (default: 'thumbnail')
 * @param boolean $icon (default: false)
 * @param string/array (default: null)
 *
 * @return string
 */
function wpse135208_cat_thumb_wrapper( $category_id = null, $size = 'thumbnail', $icon = false, $attr = null ) {
    $category_thumbnail_obj = get_the_category_data( $category_id );
    return wp_get_attachment_image( $category_thumbnail_obj->id, $size, $icon, $attr );
}
/**
 * wpse135208_cat_thumb_from_random_child.
 *
 * Get the thumbnail from a random post belonging to one of the child categories.
 *
 * @version 0.1
 *
 * @link https://wordpress.stackexchange.com/q/135208/22534
 *
 * @param integer $c_cat (default: '')
 * @param string/array $size (default: 'post-thumbnail')
 * @param string/array $attr (default: null)
 *
 * @return string
 */
function wpse135208_cat_thumb_from_random_child( $c_cat = '', $size = 'post-thumbnail', $attr = null ) {
    // do nothing if $c_cat is empty
    if( empty($c_cat) ) return;
    // get_terms is used because we only need ids
    $taxonomies = array(
        'category'
        );
    $args = array(
        'child_of' => $c_cat,
        'fields' => 'ids'
        );
    // returns an array of ids
    $child_cats = get_terms( $taxonomies, $args );
    // use this for debugging
    //echo '<pre>'; print_r($child_cats); echo '</pre>';

    $args = array(
        // we use numberposts instead of post_per_page,
        // because if the pre_get_posts filter is used,
        // it can make a difference in this case 
        // we only want one post
        'numberposts' => 1,
        // but we randomize this
        'orderby' => 'rand',
        'category__in' => $child_cats,
        'fields' => 'ids',
        // make sure only posts with featured image are considered
        'meta_query' => array(
            array(
                'key' => '_thumbnail_id',
                'compare' => 'EXISTS'
            )
        )
        );
    // returns an array containing one post id
    $ct_p_id = get_posts( $args );
    // use this for debugging
    //echo '<pre>'; print_r($ct_p_id); echo '</pre>';

    // use this for debugging
    //echo '<pre>'; print_r( ( 1 /*change to 0 to show src info*/ ) ? get_the_post_thumbnail( $ct_p_id[0], $size, $attr ) : wp_get_attachment_image_src( get_post_thumbnail_id( $ct_p_id[0] ) ) ); echo '</pre>'; 
    // now we can use this to return our thumbnail
    return get_the_post_thumbnail( $ct_p_id[0], $size, $attr );
}
/**
 * wpse135208_cat_thumb_wrapper.
 *
 * Wrapper function to have the ability to use predefined image sizes via wp_get_attachment_image().
 *
 * @version 0.1
 *
 * @link https://wordpress.stackexchange.com/q/135208/22534
 *
 * @param integer $category_id (default: null)
 * @param string/array $size (default: 'thumbnail')
 * @param boolean $icon (default: false)
 * @param string/array (default: null)
 *
 * @return string
 */
function wpse135208_cat_thumb_wrapper( $category_id = null, $size = 'thumbnail', $icon = false, $attr = null ) {
    $category_thumbnail_obj = get_the_category_data( $category_id );
    return wp_get_attachment_image( $category_thumbnail_obj->id, $size, $icon, $attr );
}
added 144 characters in body
Source Link
Nicolai Grossherr
  • 18.9k
  • 8
  • 64
  • 109
/**
 * wpse135208_cat_thumb_from_random_child.
 *
 * Get the thumbnail from a random post belonging to one of the child categories.
 *
 * @version 0.1
 *
 * @link http://wordpress.stackexchange.com/q/135208/22534
 *
 * @param integer $c_cat (default: '')
 * @param string/array $size (default: 'post-thumbnail')
 * @param string/array $attr (default: null)
 *
 * @return string
 */
function wpse135208_cat_thumb_from_random_child( $c_cat = '', $size = 'post-thumbnail', $attr = null ) {
    // do nothing if $c_cat is empty
    if( empty($c_cat) ) return;
    // get_terms is used because we only need ids
    $taxonomies = array(
        'category'
        );
    $args = array(
        'child_of' => $c_cat,
        'fields' => 'ids'
        );
    // returns an array of ids
    $child_cats = get_terms( $taxonomies, $args );
    // use this for debugging
    //echo '<pre>'; print_r($child_cats); echo '</pre>';

    $args = array(
        // we use numberposts instead of post_per_page,
        // because if the pre_get_posts filter is used,
        // it can make a difference in this case 
        // we only want one post
        'post_per_page''numberposts' => 1,
        // but we randomize this
        'order_by''orderby' => 'rand',
        'category__in' => $child_cats,
        'fields' => 'ids',
        // make sure only posts with featured image are considered
        'meta_query' => array(
            array(
                'key' => '_thumbnail_id',
                'compare' => 'EXISTS'
            )
        )
        );
    // returns an array containing one post id
    $ct_p_id = get_posts( $args );
    // use this for debugging
    //echo '<pre>'; print_r($ct_p_id); echo '</pre>';

    // use this for debugging
    //echo '<pre>'; print_r( ( 1 /*change to 0 to show src info*/ ) ? get_the_post_thumbnail( $ct_p_id[0], $size, $attr ) : wp_get_attachment_image_src( get_post_thumbnail_id( $ct_p_id[0] ) ) ); echo '</pre>'; 
    // now we can use this to return our thumbnail
    return get_the_post_thumbnail( $ct_p_id[0], $size, $attr );
}
/**
 * wpse135208_cat_thumb_from_random_child.
 *
 * Get the thumbnail from a random post belonging to one of the child categories.
 *
 * @version 0.1
 *
 * @link http://wordpress.stackexchange.com/q/135208/22534
 *
 * @param integer $c_cat (default: '')
 * @param string/array $size (default: 'post-thumbnail')
 * @param string/array $attr (default: null)
 *
 * @return string
 */
function wpse135208_cat_thumb_from_random_child( $c_cat = '', $size = 'post-thumbnail', $attr = null ) {
    // do nothing if $c_cat is empty
    if( empty($c_cat) ) return;
    // get_terms is used because we only need ids
    $taxonomies = array(
        'category'
        );
    $args = array(
        'child_of' => $c_cat,
        'fields' => 'ids'
        );
    // returns an array of ids
    $child_cats = get_terms( $taxonomies, $args );
    // use this for debugging
    //echo '<pre>'; print_r($child_cats); echo '</pre>';

    $args = array(
        // we only want one post
        'post_per_page' => 1,
        // but we randomize this
        'order_by' => 'rand',
        'category__in' => $child_cats,
        'fields' => 'ids',
        // make sure only posts with featured image are considered
        'meta_query' => array(
            array(
                'key' => '_thumbnail_id',
                'compare' => 'EXISTS'
            )
        )
        );
    // returns an array containing one post id
    $ct_p_id = get_posts( $args );
    // use this for debugging
    //echo '<pre>'; print_r($ct_p_id); echo '</pre>';

    // use this for debugging
    //echo '<pre>'; print_r( ( 1 /*change to 0 to show src info*/ ) ? get_the_post_thumbnail( $ct_p_id[0], $size, $attr ) : wp_get_attachment_image_src( get_post_thumbnail_id( $ct_p_id[0] ) ) ); echo '</pre>'; 
    // now we can use this to return our thumbnail
    return get_the_post_thumbnail( $ct_p_id[0], $size, $attr );
}
/**
 * wpse135208_cat_thumb_from_random_child.
 *
 * Get the thumbnail from a random post belonging to one of the child categories.
 *
 * @version 0.1
 *
 * @link http://wordpress.stackexchange.com/q/135208/22534
 *
 * @param integer $c_cat (default: '')
 * @param string/array $size (default: 'post-thumbnail')
 * @param string/array $attr (default: null)
 *
 * @return string
 */
function wpse135208_cat_thumb_from_random_child( $c_cat = '', $size = 'post-thumbnail', $attr = null ) {
    // do nothing if $c_cat is empty
    if( empty($c_cat) ) return;
    // get_terms is used because we only need ids
    $taxonomies = array(
        'category'
        );
    $args = array(
        'child_of' => $c_cat,
        'fields' => 'ids'
        );
    // returns an array of ids
    $child_cats = get_terms( $taxonomies, $args );
    // use this for debugging
    //echo '<pre>'; print_r($child_cats); echo '</pre>';

    $args = array(
        // we use numberposts instead of post_per_page,
        // because if the pre_get_posts filter is used,
        // it can make a difference in this case 
        // we only want one post
        'numberposts' => 1,
        // but we randomize this
        'orderby' => 'rand',
        'category__in' => $child_cats,
        'fields' => 'ids',
        // make sure only posts with featured image are considered
        'meta_query' => array(
            array(
                'key' => '_thumbnail_id',
                'compare' => 'EXISTS'
            )
        )
        );
    // returns an array containing one post id
    $ct_p_id = get_posts( $args );
    // use this for debugging
    //echo '<pre>'; print_r($ct_p_id); echo '</pre>';

    // use this for debugging
    //echo '<pre>'; print_r( ( 1 /*change to 0 to show src info*/ ) ? get_the_post_thumbnail( $ct_p_id[0], $size, $attr ) : wp_get_attachment_image_src( get_post_thumbnail_id( $ct_p_id[0] ) ) ); echo '</pre>'; 
    // now we can use this to return our thumbnail
    return get_the_post_thumbnail( $ct_p_id[0], $size, $attr );
}
added 403 characters in body
Source Link
Nicolai Grossherr
  • 18.9k
  • 8
  • 64
  • 109
Loading
added 30 characters in body
Source Link
Nicolai Grossherr
  • 18.9k
  • 8
  • 64
  • 109
Loading
added 2104 characters in body
Source Link
Nicolai Grossherr
  • 18.9k
  • 8
  • 64
  • 109
Loading
added 2104 characters in body
Source Link
Nicolai Grossherr
  • 18.9k
  • 8
  • 64
  • 109
Loading
deleted 37 characters in body
Source Link
Nicolai Grossherr
  • 18.9k
  • 8
  • 64
  • 109
Loading
added 2906 characters in body
Source Link
Nicolai Grossherr
  • 18.9k
  • 8
  • 64
  • 109
Loading
Source Link
Nicolai Grossherr
  • 18.9k
  • 8
  • 64
  • 109
Loading