Hello;
I would like to exclude featured images and multi-featured images when I insert the posts attachments into the post.
Here is the code i am using to call the post attachments:
<?php
if ( $images = get_children(array(
'post_parent' => $post->ID,
'post_type' => 'attachment',
'post_mime_type' => 'image',
'order' => 'menu_order',
'numberposts' => -1
))) : ?>
<ul>
<?php foreach( $images as $image ) : ?>
<li><?php echo wp_get_attachment_link($image->ID, 'thumbnail-latest'); ?></li>
<?php endforeach; ?>
</ul>
<?php else: // No images ?>
<!-- This post has no attached images -->
<?php endif; ?>
Here is how I am adding multiple featured images into my posts:
// Add More Images
add_theme_support( 'post-thumbnails' );
require_once('mpt/multi-post-thumbnails.php');
// Define additional "post thumbnails". Relies on MultiPostThumbnails to work
if (class_exists('MultiPostThumbnails')) {
new MultiPostThumbnails(array(
'label' => 'Featured Image (548px by 396px)',
'id' => 'workfeaturedimg',
'post_type' => 'work'
)
);
new MultiPostThumbnails(array(
'label' => 'Third Featured Image',
'id' => 'worksingleimg',
'post_type' => 'work'
)
);
};
This is how i am adding multiple featured images into my post: http://www.lifeonlars.com/wordpress/how-to-add-multiple-featured-images-in-wordpress
Thanks
Sébastien | French WordpressDesigner answers:
how create you multiple fearured images ?
Sébastien | French WordpressDesigner comments:
if your featured images have the ids : 1 and 2 and 3
the code must be
$images = get_children(
array(
'post_parent' => $post->ID,
'exclude' => array(1,2,3),
'post_status' => 'inherit',
'post_type' => 'attachment',
'post_mime_type' => 'image',
'orderby' => 'menu_order',
'order' => 'ASC'
))
Sébastien | French WordpressDesigner comments:
for the multiple featured images you use custom fields ? that's it ?
each custom field is an upload field ?
each fields use the wordpress media uploader ?
xavier comments:
I would like a programmable method, not a method that involves manually typing an ID.
Thanks
Sébastien | French WordpressDesigner comments:
$featured_images = array();
if (class_exists('MultiPostThumbnails')) {
$i=1;
while ($i<=5) {
$image_name = 'feature-image-'.$i; // sets image name as feature-image-1, feature-image-2 etc.
if (MultiPostThumbnails::has_post_thumbnail('folio', $image_name)) {
$image_id = MultiPostThumbnails::get_post_thumbnail_id( 'folio', $image_name, $post->ID );
$featured_images[]=$image_id;
}
$i++;
}
};
$images = get_children(
array(
'post_parent' => $post->ID,
'exclude' => $featured_images,
'post_status' => 'inherit',
'post_type' => 'attachment',
'post_mime_type' => 'image',
'orderby' => 'menu_order',
'order' => 'ASC'
))
Sébastien | French WordpressDesigner comments:
you use this method : http://www.lifeonlars.com/wordpress/how-to-add-multiple-featured-images-in-wordpress
so use this code (above) to exclude all multi-featured images
Sébastien | French WordpressDesigner comments:
sorry, you must add a line to exclude the first featured image
$featured_images = array();
$featured_images[]=get_post_thumbnail_id($post->ID); // <-------- this line
if (class_exists('MultiPostThumbnails')) {
$i=1;
while ($i<=5) {
$image_name = 'feature-image-'.$i; // sets image name as feature-image-1, feature-image-2 etc.
if (MultiPostThumbnails::has_post_thumbnail('folio', $image_name)) {
$image_id = MultiPostThumbnails::get_post_thumbnail_id( 'folio', $image_name, $post->ID );
$featured_images[]=$image_id;
}
$i++;
}
};
$images = get_children(
array(
'post_parent' => $post->ID,
'exclude' => $featured_images,
'post_status' => 'inherit',
'post_type' => 'attachment',
'post_mime_type' => 'image',
'orderby' => 'menu_order',
'order' => 'ASC'
))
xavier comments:
Thanks for the reply;
The below code did not work, and it still adds the featured image also.
<?php
$featured_images = array();
if (class_exists('MultiPostThumbnails')) {
$i=1;
while ($i<=5) {
$image_name = 'feature-image-'.$i; // sets image name as feature-image-1, feature-image-2 etc.
if (MultiPostThumbnails::has_post_thumbnail('folio', $image_name)) {
$image_id = MultiPostThumbnails::get_post_thumbnail_id( 'folio', $image_name, $post->ID );
$featured_images[]=$image_id;
}
$i++;
}
};
$images = get_children(
array(
'post_parent' => $post->ID,
'exclude' => $featured_images,
'post_status' => 'inherit',
'post_type' => 'attachment',
'post_mime_type' => 'image',
'orderby' => 'menu_order',
'order' => 'ASC'
))
?>
<?php foreach( $images as $image ) : ?>
<div class="imgSlide"><?php echo wp_get_attachment_link($image->ID, 'thumbnail-latest'); ?></div>
<?php endforeach; ?>
</div>
</div>
xavier comments:
i think it has something to do with this line:
$image_name = 'feature-image-'.$i; // sets image name as feature-image-1, feature-image-2 etc.
Sébastien | French WordpressDesigner comments:
in your function.php you have a code similar to
// Define additional "post thumbnails". Relies on MultiPostThumbnails to work
if (class_exists('MultiPostThumbnails')) {
new MultiPostThumbnails(array(
'label' => '2nd Feature Image',
'id' => 'feature-image-2',
'post_type' => 'folio'
)
);
new MultiPostThumbnails(array(
'label' => '3rd Feature Image',
'id' => 'feature-image-3',
'post_type' => 'folio'
)
);
new MultiPostThumbnails(array(
'label' => '4th Feature Image',
'id' => 'feature-image-4',
'post_type' => 'folio'
)
);
new MultiPostThumbnails(array(
'label' => '5th Feature Image',
'id' => 'feature-image-5',
'post_type' => 'folio'
)
);
};
could you paste this code please
xavier comments:
i renamed my ID's with a simular numbering convention to yours, thanks this did the job.
Sébastien | French WordpressDesigner comments:
ok ! cool !
Alex Sancho answers:
You can exclude any post from query just passing it's ID,
$images = get_children(array('post_parent' => $post->ID, 'exclude' => get_post_thumbnail_id($post->ID), 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'orderby' => 'menu_order', 'order' => 'ASC'))
UPDATE: http://wordpress.org/extend/plugins/multiple-post-thumbnails/ has a method to retrieve the post_thumbnail_id, so you can use the same approach to exclude this images from query from query
/**
* Retrieve Post Thumbnail ID.
*
* @param string $post_type The post type.
* @param string $id The id used to register the thumbnail.
* @param int $post_id Optional. Post ID.
* @return int
*/
public static function get_post_thumbnail_id($post_type, $id, $post_id) {
return get_post_meta($post_id, "{$post_type}_{$id}_thumbnail_id", true);
}
xavier comments:
Ok, it excluded the featured image, what about the multiple featured images ?
Julio Potier answers:
Hello
Sorry but i didn't understand the question.
:/
xavier comments:
I am calling the posts attachemnts (images) for a custom slider i am building on the post itself, i wish to exclude the featured image which i have done now following alex's code above, I also wish to exclude the multiple featured images i have set to the post also.
rizaljohn answers:
Hi xavier,
Are you using this plugin http://wordpress.org/extend/plugins/multiple-post-thumbnails/?
Where do you want to exclude the images, is that in post attachments or in multiple featured images?
xavier comments:
http://www.lifeonlars.com/wordpress/how-to-add-multiple-featured-images-in-wordpress
yes i am using this plugin in the method described in the above link
xavier comments:
i would like to exclude in the posts attachments. Thanks
rizaljohn comments:
If that's the case, then you need to modify the plugin where user can select images to exclude.