Ask your WordPress questions! Pay money and get answers fast! (more info)

Remove featured image from img gallery? WordPress

  • SOLVED

I want to remove the featured image from showing in my gallery here is a screenshot of what is happening:

As you can see the first image is the thumbnail enlarged, i don't want that to show in the gallery.

here is my code:
<div id="imgcontent">
<?php my_attachment_gallery(0, 'large', '[gallery exclude="' . get_post_thumbnail_id( $post->ID ) . '"]', 'alt="' . $post->post_title . '"'); ?>
</div>


Answers (4)

2013-08-24

Sabby Sam answers:

Hi,
We usually check if the post has thumbnail like this

<?php
// Must be inside a loop.

if ( has_post_thumbnail() ) {
the_post_thumbnail();
}
else {
echo '<img src="' . get_bloginfo( 'stylesheet_directory' ) . '/images/thumbnail-default.jpg" />';
}
?>


Could paste your whole page code here again.

<div id="imgcontent">

<?php my_attachment_gallery(0, 'large', '[gallery exclude="' . get_post_thumbnail_id( $post->ID ) . '"]', 'alt="' . $post->post_title . '"'); ?>

</div>


Emma comments:

I have posted it please check below.

2013-08-24

Arnav Joy answers:

please try this

<div id="imgcontent">

<?php my_attachment_gallery(0, 'large', '[gallery exclude="' . get_post_thumbnail_id( get_the_ID() ) . '"]', 'alt="' . $post->post_title . '"'); ?>

</div>


can you show full code please with this function's code

my_attachment_gallery


Emma comments:

I have previously used bryceadams code it worked for a while but not after i changed the featured image for the posts.

function my_attachment_gallery($postid=0, $size='thumbnail', $attributes='') {
if ($postid<1) $postid = get_the_ID();
if ($images = get_children(array(
'post_parent' => $postid,
'post_type' => 'attachment',
'orderby' => 'title',
'numberposts' => 0,
'post_mime_type' => 'image',)))
foreach($images as $image) {
$attachment=wp_get_attachment_image_src($image->ID, $size);
?><img src="<?php echo $attachment[0]; ?>" <?php echo $attributes; ?> /><?php
}

}

function mdw_exclude_thumbnail_from_gallery($null, $attr) {
if (!$thumbnail_ID = get_post_thumbnail_id())
return $null; // no point carrying on if no thumbnail ID

/* temporarily remove the filter, otherwise endless loop! */
remove_filter('post_gallery', 'mdw_exclude_thumbnail_from_gallery');

/* pop in our excluded thumbnail */
if (!isset($attr['exclude']) || empty($attr['exclude']))
$attr['exclude'] = array($thumbnail_ID);
elseif (is_array($attr['exclude']))
$attr['exclude'][] = $thumbnail_ID;

/* now manually invoke the shortcode handler */
$gallery = gallery_shortcode($attr);

/* add the filter back */
add_filter('post_gallery', 'mdw_exclude_thumbnail_from_gallery', 10, 2);

/* return output to the calling instance of gallery_shortcode() */
return $gallery;
}
add_filter('post_gallery', 'mdw_exclude_thumbnail_from_gallery', 10, 2);



Arnav Joy comments:

function my_attachment_gallery($postid=0, $size='thumbnail', $attributes='') {

if ($postid<1) $postid = get_the_ID();

if ($images = get_children(array(

'post_parent' => $postid,

'post_type' => 'attachment',

'orderby' => 'title',

'numberposts' => 0,

'exclude' => get_post_thumbnail_id( $post->ID ),

'post_mime_type' => 'image',)))

foreach($images as $image) {

$attachment=wp_get_attachment_image_src($image->ID, $size);

?><img src="<?php echo $attachment[0]; ?>" <?php echo $attributes; ?> /><?php

}



}


Emma comments:

Didn't work.


Emma comments:

I have checked my source and it says
<img ]="" exclude="110" [gallery="" src="http://wbesitename.net/wp-content/uploads/2013/08/image_034.jpg" style="display: inline;"></img>


Arnav Joy comments:

Try this


function my_attachment_gallery($postid=0, $size='thumbnail', $attributes='') {

if ($postid<1) $postid = get_the_ID();

if ($images = get_children(array(

'post_parent' => $postid,

'post_type' => 'attachment',

'orderby' => 'title',

'numberposts' => 0,

'exclude' => get_post_thumbnail_id( get_the_ID () ),

'post_mime_type' => 'image',)))

foreach($images as $image) {

$attachment=wp_get_attachment_image_src($image->ID, $size);

?><img src="<?php echo $attachment[0]; ?>" <?php echo $attributes; ?> /><?php

}



}


Emma comments:

Yes that was perfect!!


Arnav Joy comments:

Glad to hear that , please vote now ane close the question

2013-08-24

Abdelhadi Touil answers:

Hi.
What about this code here:

[[LINK href="http://bryceadams.com/exclude-featured-image-from-gallery-shortcode/"]]http://bryceadams.com/exclude-featured-image-from-gallery-shortcode/[[/LINK]]


Emma comments:

I tried this previously but it didn't work.

2013-08-24

Liam Bailey answers:

Can you please post the code of my_attachment_gallery