I have the following code in my functions.php:
开发者_StackOverflowfunction get_images($size = 'thumbnail') {
global $post;
return get_children( array('post_parent' => get_the_ID(), 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => 'ASC', 'orderby' => 'menu_order ID') );
}
And in my single.php
<?php $photos = get_images('full'); ?>
<?php $x = 0; ?>
<?php foreach($photos as $photo): ?>
<?php if($x < 1): ?>
<img src="<?=wp_get_attachment_url($photo->ID)?>" alt="fullImg" />
<?php endif; ?>
<?php $x++;
?>
<?php endforeach; ?>
I want to show just ONE image there, but it shows me also the post-thumb image, which I don't want, is there an option to exclude that?
That's some crazy coding there! It's pointless accepting a size in your function, because it merely returns an array of post objects.
Once you've got your posts, then use the appropriate attachment function(s) to get the information on the attachment size you're after.
I would propose a function like this instead;
function get_images($overrides = '', $exclude_thumbnail = false)
{
return get_posts(wp_parse_args($overrides, array(
'numberposts' => -1,
'post_parent' => get_the_ID(),
'post_type' => 'attachment',
'post_mime_type' => 'image',
'order' => 'ASC',
'exclude' => $exclude_thumbnail ? array(get_post_thumbnail_id()) : array(),
'orderby' => 'menu_order ID'
)));
}
And putting it in practice;
<?php if ($photo = get_images('numberposts=1', true)): ?>
<img src="<?php echo wp_get_attachment_url($photo[0]->ID); ?>" alt="fullimg" />
<?php endif; ?>
UPDATE: Typo in function - fixed.
精彩评论