• Hiya to All,

    I’m using a code snippet for displaying the first attachment image for excerpts on the homepage – to have a smarter and more logical code, I actually try to use this snippet as a function – but somehow I can’t.

    There are different sections on the homepage for each day: a main section for the current day, and four smaller sections for the other weekdays – these latters only display post excerpts.

    Well, if I repeat the same snippet in the four smaller sections, it works fine. However, when I try to use the function, the four callings of the function returns four times the image belonging to the first section.
    I have a doubt that I miss something related to arguments, but not sure what and how.

    The repeated snippet is:

    $args = array(
                                    'order'          => 'ASC',
                                    'post_type'      => 'attachment',
                                    'post_parent'    => $post->ID,
                                    'post_mime_type' => 'image',
                                    'post_status'    => null,
                                    'numberposts'    => 1,
                                    );
    
                                    $attachments = get_posts($args);
    
                                    if ($attachments) {
                                        foreach ($attachments as $attachment) {
                                            echo '<div class="eimages">' . wp_get_attachment_link($attachment->ID, 'thumbnail', false, false) . '</div>';
                                            }
                                        }
                                    else { echo '<p class="center">a ' . strtolower($post->dayname) . ' with no image :(</p>'; };

    When I try to “convert” this into a function, I use the following code:

    function excerptImage() {
    	$args = array(
    	'order'          => 'ASC',
    	'post_type'      => 'attachment',
    	'post_parent'    => $post->ID,
    	'post_mime_type' => 'image',
    	'post_status'    => null,
    	'numberposts'    => 1,
    	);
    
    	$attachments = get_posts($args);
    
    	if ($attachments) {
    		foreach ($attachments as $attachment) {
    			echo '<div class="eimages">' . wp_get_attachment_link($attachment->ID, 'thumbnail', false, false) . '</div>';
    			}
    		}
    	else { echo '<p class="center">a ' . strtolower($post->dayname) . ' with no image :(</p>'; };	
    
    }

    And I’m calling it in the page’s php code by using simply

    <?php excerptImage(); ?>

    Could you please help me with this? Do I miss the ‘argumentation’ of the function? Or what would cause that the function doesn’t return the actual excerpt’s attachment image, but that of the first excerpt – four times…

    Thanks in advance for any kind of help.

Viewing 2 replies - 1 through 2 (of 2 total)
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘issue with using a snippet (displaying excerpt images) as a function’ is closed to new replies.