WordPress: erstes Artikelbild anzeigen

Um nur das erste Bild, der einem Artikel zugeordneten Bilder anzuzeigen, eigentlich sich dieser Code:

In der functions.php:

1
2
3
4
5
6
7
8
9
10
11
12
function my_theme_get_post_image($size = 'thumbnail') {
    global $post;
 
    $photos = get_children( array('post_parent' => $post->ID, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => 'ASC', 'orderby' => 'menu_order ID') );
    
    if ($photos) {
        $photo = array_shift($photos);
        return wp_get_attachment_image($photo->ID, $size);
    }
    
    return false;
}
function my_theme_get_post_image($size = 'thumbnail') {
	global $post;

	$photos = get_children( array('post_parent' => $post->ID, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => 'ASC', 'orderby' => 'menu_order ID') );
	
	if ($photos) {
		$photo = array_shift($photos);
		return wp_get_attachment_image($photo->ID, $size);
	}
	
	return false;
}

Aufrufen lässt sich das Bild dann im Theme mit:

1
$photo = my_theme_get_post_image();
$photo = my_theme_get_post_image();

Credits an John Ford für diesen Code.

Sebastian Galleitner

Autor: Sebastian Galleitner

Sebastian Galleitner ist Senior Webdesigner bei woont-solutions und Inhaber der Zielgestalt Online Agentur.

Leave a Reply

  • (will not be published)

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code lang=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" extra="">