• Resolved bobbybosler

    (@bobbybosler)


    Hey there, one more thing. Is there an easy way to create a shortcode or function that will let me return the url of the latest sermon image via a shortcode? I would like to call just shortcode in some of my inline css.

Viewing 1 replies (of 1 total)
  • Hi,

    I did not test this code, but this should work:

    /**
     * Gets the latest sermon image URL.
     *
     * @return false|string Image URL or false if there is no image or sermon.
     */
    function get_latest_sermon_image_url() {
    	$query = new WP_Query( array(
    		'post_type'      => 'wpfc_sermon',
    		'meta_key'       => 'sermon_date',
    		'meta_value_num' => time(),
    		'meta_compare'   => '<=',
    		'orderby'        => 'meta_value_num',
    		'order'          => 'DESC',
    		'posts_per_page' => 1,
    	) );
    
    	if ( $query->have_posts() ) {
    		$query->the_post();
    		global $post;
    
    		$thumbnail_id = get_post_thumbnail_id( $post );
    
    		if ( $thumbnail_id ) {
    			return wp_get_attachment_image_url( $thumbnail_id, 'post-thumbnail' );
    		}
    	}
    
    	return false;
    }
    

    Thank you for your patience.

Viewing 1 replies (of 1 total)

The topic ‘get_latest_sermon_image_url()’ is closed to new replies.