• Hi,

    I use the following function in functions.php to output all images of a post’s gallery:

    <?php
    function my_attachment_image($postid=0, $size='large', $attributes='') {
    	if ($postid<1) $postid = get_the_ID();
    	if ($images = get_children(array(
    		'post_parent' => $postid,
    		'post_type' => 'attachment',
    		'numberposts' => -1,
    		'orderby' => 'menu_order',
    		'post_mime_type' => 'image',)))
    		foreach($images as $image) {
    			$attachment=wp_get_attachment_image_src($image->ID, $size);
    			?><a href="<?php echo $attachment[0]; ?>" <?php echo $attributes; ?> rel="fancybox-<?php echo $postid; ?>">&nbsp;</a><?php
    		}
    }?>

    The only thing missing from this function for me right now it the ability to exclude a specific image. That specific image would be the one with number 1 manually specified in the order field of the gallery.

    I tried adding this with no luck:

    ‘exclude’ => ‘1’,

    Many thanks for your time and help.

Viewing 4 replies - 1 through 4 (of 4 total)
  • Maybe this will work

    $to_exclude = 3;
    $i = 1;
    foreach($images as $image) {
    $attachment=wp_get_attachment_image_src($image->ID, $size);
    if( $i != $to_exclude ){
    ?><a href="<?php echo $attachment[0]; ?>" <?php echo $attributes; ?> rel="fancybox-<?php echo $postid; ?>">&nbsp;</a><?php
    }
    $i++
    }

    Thread Starter supergab

    (@supergab)

    I get syntax error

    <?php
    function my_attachment_image($postid=0, $size='full', $attributes='') {
    	if ($postid<1) $postid = get_the_ID();
    	if ($images = get_children(array(
    		'post_parent' => $postid,
    		'post_type' => 'attachment',
    		'numberposts' => -1,
    		'orderby' => 'menu_order',
    		'exclude' => '1',
    		'post_mime_type' => 'image',)))
    		$to_exclude = 1;
    		$i = 1;
    		foreach($images as $image) {
    			$attachment=wp_get_attachment_image_src($image->ID, $size);
    			if( $i != $to_exclude ){
    			?><a href="<?php echo $attachment[0]; ?>" <?php echo $attributes; ?> rel="fancybox-<?php echo $postid; ?>">&nbsp;</a><?php
    		}
    		$i++
    }?>

    Could you guide me please ?

    $i++;

    Thread Starter supergab

    (@supergab)

    Thank you

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Excluding a specific image from get_children according to its order number ?’ is closed to new replies.