Revision: 39258
Updated Code
at January 17, 2011 17:07 by Wendelboe
Updated Code
<?php //Put this in functions.php function get_primary_image($id, $size){ $featured = wp_get_attachment_image_src( get_post_thumbnail_id($id), $size, false); if($featured){ $childURL = $featured['0']; }else{ $children = get_children(array('post_parent' => $id, 'post_type' => 'attachment', 'post_mime_type' => 'image', 'numberposts' => 1)); reset($children); $childID = key($children); //$childURL = wp_get_attachment_url($childID); $childArray = wp_get_attachment_image_src($childID, $size, false); $childURL = $childArray['0']; if(empty($childURL)){ $childURL = get_bloginfo('template_url')."/images/default.png"; } } return($childURL); } //Run this in the loop (or any place you'd like - as long as you have an ID to feed it..) //First argument is the ID.. //Second argument is the size.. It'll handle 'large', 'medium', 'thumbnail' or even 'array(100, 100)'.. get_primary_image(get_the_ID(), 'large'); ?>
Revision: 39257
Initial Code
Initial URL
Initial Description
Initial Title
Initial Tags
Initial Language
at January 15, 2011 08:13 by Wendelboe
Initial Code
<?php //Put this in functions.php function get_primary_image($id, $size){ $featured = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), $size, false); if($featured){ $childURL = $featured['0']; }else{ $children = get_children(array('post_parent' => $id, 'post_type' => 'attachment', 'post_mime_type' => 'image', 'numberposts' => 1)); reset($children); $childID = key($children); //$childURL = wp_get_attachment_url($childID); $childArray = wp_get_attachment_image_src($childID, $size, false); $childURL = $childArray['0']; if(empty($childURL)){ $childURL = get_bloginfo('template_url')."/images/default.png"; } } return($childURL); } //Run this in the loop (or any place you'd like - as long as you have an ID to feed it..) //First argument is the ID.. //Second argument is the size.. It'll handle 'large', 'medium', 'thumbnail' or even 'array(100, 100)'.. get_primary_image(get_the_ID(), 'large'); ?>
Initial URL
Initial Description
If post has featured image, use it.. If not, pick out the first in the post.. Still not working? Stick with the default one..
Initial Title
Get primary image for Wordpress post
Initial Tags
image, wordpress
Initial Language
PHP