WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] how do I reference an uploaded post image using code? (10 posts)

  1. ibexy
    Member
    Posted 4 years ago #

    I am building a custom posts + excerpt listing. I dont know how to pull uploaded/attached post images. Any help please?

  2. Mark / t31os
    Moderator
    Posted 4 years ago #

  3. Shane G
    Member
    Posted 4 years ago #

    Hi,

    Have a check with this plugin:

    http://wordpress.org/extend/plugins/image-link/

    Thanks,

    Shane G.

  4. ibexy
    Member
    Posted 4 years ago #

    Thanks
    I found the following code but it returns every image from the gallery and not ONLY THE IMAGE attached to the current post. This is my code below. I want to return only the image assiciated with the current post in the loop.Basically the below prints an excerpt and am triying to extract the attached image to be like a thumbnail. Any help please?

    <?php $excerpt = get_the_excerpt();
                echo $excerpt; ?>   
    
    <?php $thumbnailimages =& get_children( 'post_type=attachment&post_mime_type=image' );
    
                    if ( empty($thumbnailimages) ) {
                      // no attachments here
                    } else {
    
                      foreach ( $thumbnailimages as $attachment_id => $attachment ) {
                        echo wp_get_attachment_image( $attachment_id, 'full' );
                      }
                    }
              ?>
  5. Mark / t31os
    Moderator
    Posted 4 years ago #

    See the parameters on the link i gave you..
    http://codex.wordpress.org/Function_Reference/get_children

    Namingly post_parent and numberposts..

    Attachments are children of the post they exist in, so feed the post ID into the post parent parameter..

  6. ibexy
    Member
    Posted 4 years ago #

    Thanks for your response. Am a bit lost with all this tech. talk :) I'd really appreaciate if you could help further. I have inserted my full archive.php here hoping you could help. Currently this file lists the excerpts. I just need to insert a thumbnail of the attached images using the above or other means. Thanks.

    <?php get_header(); ?>
    <?php get_sidebar(); ?>
    <div class="archive2">
      <?php $odd_or_even = 'odd'; ?>
      <?php if (have_posts()) : ?>
      <?php while (have_posts()) : the_post(); // check for product large image
    $pli = get_post_meta($post->ID, 'pli', $single = true); // check for item price
    $price = get_post_meta($post->ID, 'price', $single = true);
     ?>
      <div class="post-<?php echo $odd_or_even; ?>" id="post-<?php the_ID(); ?>">
        <?php $odd_or_even = ('odd'==$odd_or_even) ? 'even' : 'odd'; ?>
        <div class="col1">
          <h2><a href="<?php the_permalink() ?>" rel="bookmark" title="<?php echo THEME_PERMANENT_LINK; ?><?php the_title_attribute(); ?>">
            <?php the_title(); ?>
            </a></h2>
    
    <!-- THIS IS THE EXCERPT HERE -->
    
                <?php $excerpt = strip_tags(get_the_excerpt());
                echo $excerpt; ?>           
    
          <p><img src="<?php bloginfo('template_directory'); ?>/imgs/page.png" alt="<?php echo THEME_CATEGORIES; ?>" /> <strong><?php echo THEME_CATEGORIES; ?></strong>
            <?php the_category(' '); ?>
          <p>
          <p><img src="<?php bloginfo('template_directory'); ?>/imgs/tag.png" alt="<?php echo THEME_TAGS; ?>" /> <strong><?php echo THEME_TAGS; ?></strong>
            <?php the_tags(' '); ?>
          <p>
        </div>
    
        <div style="clear: both"></div>
      </div>
      <?php endwhile; ?>
      <div class="navigation">
        <div class="left">
          <?php next_posts_link(THEME_POST_ENTRIES_PREV) ?>
        </div>
        <div class="right">
          <?php previous_posts_link(THEME_POST_ENTRIES_NEXT) ?>
        </div>
      </div>
      <?php else : ?>
      <h2 class="center"><?php echo THEME_POST_NOT_FOUND; ?></h2>
      <p class="center"><?php echo THEME_POST_NOT_FOUND_MSG; ?></p>
      <?php include (TEMPLATEPATH . "/searchform.php"); ?>
      <?php endif; ?>
    </div>
    
    <?php get_footer(); ?>
  7. Mark / t31os
    Moderator
    Posted 4 years ago #

    Try this..

    <?php get_header(); ?>
    <?php get_sidebar(); ?>
    <div class="archive2">
    	<?php $odd_or_even = 'odd'; ?>
    	<?php if (have_posts()) : ?>
    	<?php while (have_posts()) : the_post(); // check for product large image
    	$pli = get_post_meta($post->ID, 'pli', true); // check for item price
    	$price = get_post_meta($post->ID, 'price', true);
    	?>
    	<div class="post-<?php echo $odd_or_even; ?>" id="post-<?php the_ID(); ?>">
    		<?php $odd_or_even = ('odd'==$odd_or_even) ? 'even' : 'odd'; ?>
    		<div class="col1">
    			<h2>
    				<a href="<?php the_permalink() ?>" rel="bookmark" title="<?php echo THEME_PERMANENT_LINK; ?><?php the_title_attribute(); ?>">
    				<?php the_title(); ?>
    				</a>
    			</h2>
    			<?php
    			$args = array(
    				'post_parent' => $post->ID,
    				'post_type' => 'attachment',
    				'numberposts' => 1,
    				'post_status' => 'any',
    				'post_mime_type' => 'image'
    			);
    			$thumbnailimages =&get_children( $args );
    			if ( !empty($thumbnailimages) ) {
    				foreach ( $thumbnailimages as $attachment_id => $attachment ) echo wp_get_attachment_image( $attachment_id, 'thumbnail' );
    			}
    			?>
    			<!-- THIS IS THE EXCERPT HERE -->
    			<?php echo strip_tags(get_the_excerpt()); ?>
    			<p><img src="<?php bloginfo('template_directory'); ?>/imgs/page.png" alt="<?php echo THEME_CATEGORIES; ?>" /> <strong><?php echo THEME_CATEGORIES; ?></strong>
    			  <?php the_category(' '); ?>
    			</p>
    			<p><img src="<?php bloginfo('template_directory'); ?>/imgs/tag.png" alt="<?php echo THEME_TAGS; ?>" /> <strong><?php echo THEME_TAGS; ?></strong>
    			  <?php the_tags(' '); ?>
    			</p>
    		</div>
    		<div style="clear: both"></div>
    	</div>
    	<?php endwhile; ?>
    	<div class="navigation">
    		<div class="left"><?php next_posts_link(THEME_POST_ENTRIES_PREV) ?></div>
    		<div class="right"><?php previous_posts_link(THEME_POST_ENTRIES_NEXT) ?></div>
    	</div>
    	<?php else : ?>
    	<h2 class="center"><?php echo THEME_POST_NOT_FOUND; ?></h2>
    	<p class="center"><?php echo THEME_POST_NOT_FOUND_MSG; ?></p>
    	<?php // include (TEMPLATEPATH . "/searchform.php"); ?>
    	<?php get_search_form(); // Does the same as above line ?>
    	<?php endif; ?>
    </div>
    <?php get_footer(); ?>
  8. ibexy
    Member
    Posted 4 years ago #

    it worked perfectly! Thanks very much!

    I added a <div> around the image to enable me add a float:left CSS. Its showing OK except a few images are not coming up. Dont know why but am ok with this result.

    How do I scale all the images to say a uniform 120x120. Is ther any way to do this in CSS? I have tried the width and height statements. They dont work.

  9. Mark / t31os
    Moderator
    Posted 4 years ago #

    Set the thumbnail size to 120 x 120?

    If you have uploaded images already you'll need to re-add them again under the new settings.

  10. ibexy
    Member
    Posted 4 years ago #

    Thanks.

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags