Pull latest attachment from post?
-
Hi,
Is there get attachment function that would allow me grab the latest attachment during the loop?
-
*bump ~ help
I list ten other articles at the end of a post, where I would like to show an icon image next to the title.
What code are you using to list the 10 articles?
<?php query_posts('showposts=10&offset=3'); ?> <!-- 10 most recent posts --> <ul> <h2>Other Articles</h2> <?php while (have_posts()) : the_post(); ?> <li><span class="title"><a href="<?php the_permalink() ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></span> | <span class="comment"><a href="<?php the_permalink() ?>#commenting" title="Jump to the comments"><?php comments_number('0 Comments','1 Comment','% Comments'); ?></a></span>| <span class="meta"> - <?php the_time('l, F j, Y G:i'); ?></span></li> <?php endwhile; ?> </ul>
I needed a good pointer on how to display image attachments for posts from a particular category, and am very thankful for the function posted by ‘mores’ above. I made some minor change so that I can query just the category I want and also to remove the link on each image as this is just a slideshow of the full size images:
function categoryImages($size=large,$catID) { $posts=query_posts('cat='.$catID); foreach( $posts as $post ) { if ( $images = get_children(array( 'post_parent' => $post->ID, 'post_type' => 'attachment', 'numberposts' => -1, 'post_mime_type' => 'image',))) { foreach( $images as $image ) { $attachmenturl=wp_get_attachment_url($image->ID); $attachmentimage=wp_get_attachment_image( $image->ID, $size ); echo $attachmentimage; } } else { echo "No Images"; } } }
The function to place in your theme template is:
<?php categoryImages('large',22); ?>
…or whatever your category number is.
I list ten other articles at the end of a post, where I would like to show an icon image next to the title
Try something like this (there are a lot of “post image” functions):
<?php the_post_image(); ?>
function the_post_image($size = 'thumbnail') { $id = get_the_ID(); if ( empty($id) ) { trigger_error( __FUNCTION__ . "(): Couldn't get current post ID", E_USER_WARNING); return false; } $images = get_children(array( 'numberposts' => 1, 'post_parent' => $id, 'post_type' => 'attachment', 'post_mime_type' => 'image', )); if ( empty($images) ) { // Post has no attached images return false; } $image = array_pop($images); $output = wp_get_attachment_image($image->ID, $size); if ( empty($output) ) { trigger_error( __FUNCTION__ . "(): Couldn't print image for attachment #{$image->ID}", E_USER_WARNING); return false; } echo $output; }
You can use it inside the hyperlink in the “Other Articles” loop:
… <a href="<?php the_permalink() ?>"><?php the_post_image(); ?> <?php the_title(); ?></a> …
Put the
post_image()
function definition in your template or in your theme’s functions.php file.Thank you so much Sam_a! I really appreciate it. I got the link and the image working for the first article, unfortunately it doesn’t display images for the rest of the remaining nine posts in the list.
I wonder what I am doing wrong…
Here is the full code in a nutshell… does anyone have any idea why images dont show up for the rest of the titles in the list.
<?php get_header(); ?> <div id="featured"> <?php query_posts('showposts=3'); ?> <?php while (have_posts()) : the_post(); ?> <h2><?php the_category(', '); ?><i> - <?php the_time('l, F j, Y G:i'); ?> - <a href="<?php the_permalink() ?>#commenting" title="Jump to the comments"><?php comments_number('0 Comments','1 Comment','% Comments'); ?></a></i></h2> <h1><a href="<?php the_permalink() ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></h1> <?php the_content('Continue...'); ?> <?php endwhile; ?> <div class="clear"></div> </div> <!-- END --> <?php query_posts('showposts=10&offset=3'); ?> <!-- 10 most recent posts --> <ul> <h2>Other Articles</h2> <?php while (have_posts()) : the_post(); ?> <li><a href="<?php the_permalink() ?>"><?php the_post_image(); ?></a><span class="title"><a href="<?php the_permalink() ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></span> | <span class="comment"><a href="<?php the_permalink() ?>#commenting" title="Jump to the comments"><?php comments_number('0 Comments','1 Comment','% Comments'); ?></a></span><span class="meta"> - <?php the_time('l, F j, Y G:i'); ?></span></li> <?php endwhile; ?> </ul> <p class="link"><a href="<?php echo get_option('home'); ?>/archives" title="View Archives">archives</a> </div> <div class="column_mainbottom">.</div> </div> <!-- END --> <?php get_sidebar(); ?> <!-- Calling the sidebar with the AJAX box, or the widgets --> </div> <?php get_footer(); ?>
@gambit37 … you saved my day.
I managed to create a combined version of my code and your category-sensitive modification (because I think I need variable number of images).This saved my project. I got all kinds of images on the front page, logos and buttons etc. that really threatened to make me look bad in the eyes of my customer ??
Do you have a website with a “donate” button?
I have a website, but no donate button. ?? Not to worry, I’m glad to help.
well thank you very much for helping. Great community, this is!
I noticed, this way of going through the posts of a category will give me X number of images randomly TIMES Y NUMBER OF POSTS in that category.
Is there a way to sort of turn the whole thing upside down and have it go through the posts randomly, select one random image, until the desired number of images has been displayed?
This is not life-threatening, since I can manage to fake the whole thing by knowing the number of posts and using a div with “overwrite:none” to hide the excess pics. But it would be interesting to learn, maybe for a next project, or someone else in this community.I got the code to work on the index.php and single.php… great stuff.
Is there anyway to make it work in attachment.php/image.php? Thanks
function postimage($size=medium) { if ( $images = get_children(array( 'post_parent' => get_the_ID(), 'post_type' => 'attachment', 'numberposts' => 1, 'post_mime_type' => 'image',))) { foreach( $images as $image ) { $attachmenturl=wp_get_attachment_url($image->ID); $attachmentimage=wp_get_attachment_image( $image->ID, $size ); echo '<a href="'.$attachmenturl.'" rel="lightbox">'.$attachmentimage.'</a>'; } } else { echo "No Image"; } }
Using the above code by Mores… is it possible to make it “count” before executing the echo?
I want it to pull images relevant to the post to display on the index as thumbnails, but only if there are x number of images available.
Thanks for the pointers on this thread, really helpful!
I am trying to display a link to the first attached mp3 on a post, but so that it says simply ‘Download’, not the actual title/filename of the mp3…
But none of the above allow this as far as I can tell?
I could change the name of every mp3 to ‘Download’ but that seems a hack too far! ??
Got it!
<?php global $wpdb; $attachment_id = $wpdb->get_var("SELECT ID FROM $wpdb->posts WHERE post_parent = '$post->ID' AND post_status = 'inherit' AND post_type='attachment' ORDER BY post_date DESC LIMIT 1"); ?> <?php if ($attachment_id) echo '<a href=' . wp_get_attachment_url($attachment_id) . '>Download</a>'; ?>
- The topic ‘Pull latest attachment from post?’ is closed to new replies.