Ejemplo n.º 1
0
/**
 * Returns the first image from the post content for a image post
 * and the first image from the gallery for a gallery post.
 *
 * @param $size , $post
 *
 * @return void
 */
function hannover_image_from_gallery_or_image_post($size, $post)
{
    if (has_post_thumbnail()) {
        the_post_thumbnail($size);
    } else {
        $post_format = get_post_format($post);
        if ($post_format == 'gallery') {
            $images = hannover_get_gallery_images($post->ID);
            $image = array_shift($images);
            $img_tag = wp_get_attachment_image($image->ID, $size);
        } elseif ($post_format == 'image') {
            $first_img_url = hannover_get_first_image_from_post_content();
            $pattern = '/-\\d+x\\d+(\\.\\w{3,4}$)/i';
            $first_img_url = preg_replace($pattern, '${1}', $first_img_url);
            $first_img_id = attachment_url_to_postid($first_img_url);
            if ($first_img_id == 0) {
                $attachments = get_children(array('post_parent' => $post->ID, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => 'ASC', 'orderby' => 'menu_order'));
                $first_img = array_shift($attachments);
                $img_tag = wp_get_attachment_image($first_img->ID, $size);
            } else {
                $img_tag = wp_get_attachment_image($first_img_id, $size);
            }
        }
        echo $img_tag;
    }
}
<?php

/**
 * Template Name: Front page with random image
 *
 * @version 1.0
 */
get_header();
?>
	<main role="main">
		<?php 
$images = hannover_get_gallery_images($post->ID);
shuffle($images);
$first_image = array_shift($images);
?>
		<figure>
			<?php 
if (!empty($first_image)) {
    echo wp_get_attachment_image($first_image->ID, 'large');
}
if (!empty($first_image->post_excerpt)) {
    ?>
				<figcaption>
					<?php 
    echo $first_image->post_excerpt;
    ?>
				</figcaption>
			<?php 
}
?>
		</figure>