WordPress.org

Ready to get started?Download WordPress

Forums

Can I Pull the first full image out of post gallery inside current post loop (10 posts)

  1. mkjar
    Member
    Posted 3 years ago #

    I don't know if this is entirely possible - but I am building a single post template for a specific category (I am using the template for individual case studies in my portfolio.)

    But what I am stuck on is I want one full size image that is pulled automatically from the post media gallery into the template layout. I want it to be within the post loop but hard coded into the in the template php (NOT short-coded in the entry content later) . My goal is that I want someone else who is not as comfortable with wp to be able to type in the text in the post box - upload the images to the gallery (but not insert them into the post) and when they select the right category and the images & text will automatically appear how I styled them on that and every category's single post pages . Is this doable?

    sorry for the long explanation - below is what I have now : it is loading a stretched out thumbnail - so it is the right concept but i want it to catch the first full image in the post image gallery.

    backticks

    <?php if (have_posts()) : while (have_posts()) : the_post(); ?>
    <div class="post" id="post-<?php the_ID(); ?>">
    
    <!------- this code below pulls out the thumbnail sized image and stretches it ----->
    <?php $images =& get_children('post_type=attachment&post_mime_type=image&post_parent=' . $post->ID );
    $firstImageSrc = wp_get_attachment_image_src(array_shift(array_keys($images)));
    echo "<img src=\"{$firstImageSrc[0]}\" width=\"500\" height=\"300\" />"; ?>
    
    <!---- the gallery shortcode works and i am using Images Fancified plugin--->
    <?php echo do_shortcode('[gallery]'); ?>
    
    <h2><?php the_title(); ?></h2>
    <div class="entry">
    <?php the_content('<p class="serif">Read the rest of this entry &raquo;</p>'); ?>
    <?php wp_link_pages(array('before' => '<p><strong>Pages:</strong> ', 'after' => '</p>', 'next_or_number' => 'number')); ?>
    
    <div class="features">
    <?php the_meta(); ?>
    </div><!--/features-->
    
    <?php endwhile; else: ?>
    <p>Sorry, no posts matched your criteria.</p>
    <?php endif; ?>

    backticks

  2. esmi
    Forum Moderator
    Posted 3 years ago #

  3. mkjar
    Member
    Posted 3 years ago #

    Thanks!

    I am still having problems with calling up the full image I am still only getting the thumbnail image. any hints?

    '<?php

    $args = array(
    'post_type' => 'attachment',
    'numberposts' => 1,
    'post_status' => null,
    'post_parent' => $post->ID
    );
    $attachments = get_posts($args);
    if ($attachments) {
    foreach ($attachments as $attachment) {
    echo apply_filters('the_title', $attachment->post_title);
    the_attachment_link($attachment->ID, false);
    }
    }

    ?>
    '

    thanks again!

  4. esmi
    Forum Moderator
    Posted 3 years ago #

    Use wp_get_attachment_link - not the_attachment_link.

  5. mkjar
    Member
    Posted 3 years ago #

    do you mean like this:

    <?php
    $args = array(
    	'post_type' => 'attachment',
    	'numberposts' => 1,
    	'post_status' => null,
    	'post_parent' => $post->ID
    	);
    $attachments = get_posts($args);
    if ($attachments) {
    	foreach ($attachments as $attachment) {
    		echo apply_filters('the_title', $attachment->post_title);
    		wp_get_attachment_link($attachment->ID, false);
    	}
    }
    ?>

    Because now that pulls up only the image title but not the image itself- am I missing somthing?

    thanks!

  6. esmi
    Forum Moderator
    Posted 3 years ago #

    Try using:

    echo wp_get_attachment_image(k($attachment->ID, 'full');

    http://codex.wordpress.org/Function_Reference/wp_get_attachment_image

  7. mkjar
    Member
    Posted 3 years ago #

    That seems to produce Parse error: syntax error, unexpected ';'

  8. esmi
    Forum Moderator
    Posted 3 years ago #

    An extra character or 2 seems to gotten into the line. Try:

    echo wp_get_attachment_image($attachment->ID, 'full');

  9. mkjar
    Member
    Posted 3 years ago #

    hmms - now it doesn't produce any errors but doesn't produce an image either...

  10. kelune
    Member
    Posted 3 years ago #

    <?php
    $args = array(
    	'post_type' => 'attachment',
    	'numberposts' => 1,
    	'post_status' => null,
    	'post_parent' => $post->ID
    	);
    $attachments = get_posts($args);
    if ($attachments) {
    	foreach ($attachments as $attachment) {
    		echo apply_filters('the_title', $attachment->post_title);
    		the_attachment_link($attachment->ID, false);
    	}
    }
    ?>

Topic Closed

This topic has been closed to new replies.

About this Topic