WordPress.org

Ready to get started?Download WordPress

Forums

Excluding a specific image from get_children according to its order number ? (5 posts)

  1. supergab
    Member
    Posted 3 years ago #

    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.

  2. Reuben Gunday
    Member
    Posted 3 years ago #

    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++
    }
  3. supergab
    Member
    Posted 3 years ago #

    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 ?

  4. Reuben Gunday
    Member
    Posted 3 years ago #

    $i++;

  5. supergab
    Member
    Posted 3 years ago #

    Thank you

Topic Closed

This topic has been closed to new replies.

About this Topic