WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Pull latest attachment from post? (49 posts)

  1. 3stripe
    Member
    Posted 5 years ago #

    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>'; ?>
  2. omgitztrey
    Member
    Posted 5 years ago #

    FYI... not to toot my own site or whatever, but I put some of the coding referenced here into action.

    TREYCRUZ.COM

  3. grid
    Member
    Posted 5 years ago #

    Wow, this is a great thread. Someone should write a sidebar widget that shows the last X posts with thumbnails. (Maybe that someone is me!) I'll search to make sure someone hasn't already done it first tho.

  4. omgitztrey
    Member
    Posted 5 years ago #

    Well there's already some coding on page1 for random post... I suppose just get rid of the RAND() variable? lol I unno.

  5. 007casper
    Member
    Posted 5 years ago #

    regarding "Other Articles" loop

    I have placed the code as Sam_a suggested in functions.php and in my index.php loop... but somehow I only get the first article on the list to show up ~ not the rest

    does anyone know why I am not getting images to show up for the rest of the nine articles?

    thanks

  6. mores
    Member
    Posted 5 years ago #

    007casper: look for the "numberposts => 1" bit in your code.
    adapt accordingly. If you do not want a fixed number of posts to show up, use "-1" to display all.

    Okay, while we're doing so well here, I got a brand new question:

    By using the WP gallery, you can set the order in which the images are supposed to be displayed in that gallery.
    Now where is that information stored, and how can I use that gallery sorting order to order my manually extracted images?

  7. 007casper
    Member
    Posted 5 years ago #

    I have tried that "numberposts => 1" changed to "numberposts => -1," no luck... I will appreciate it if anyone could help me regarding this...

    thank you

  8. Sam_a
    Member
    Posted 5 years ago #

    Hello 007casper,

    You may want to start a new thread since this one is already "resolved". Anyway --

    I tried the full code on my WP and it prints images for every post that has an image.

    If you're sure that each post has an attached image and a working thumbnail, do "View source" from your browser and check if WP is printing an <img /> (even if you can't see it on the page).

    You don't want to change numberposts => 1 in the post_image() function because it's only supposed to print 1 image per post.

  9. omgitztrey
    Member
    Posted 5 years ago #

    ANOTHER THING :D

    How can we pull out the images by date?

    The reason I want to do this is because i'm creating some new thumbnails with the next theme launch that is size specific and I only want it to pull images that are after ie: August 1, 2008 or whatever.

  10. mores
    Member
    Posted 5 years ago #

    you add a "where post_date > August 1, 2008".
    The problem here is actually working with the date. It's probably coded all weird in the database and you need to convert your desired date to this weird code that starts win 1971 or something.

  11. allancole
    Member
    Posted 5 years ago #

    This thread has been awesome!

    I am now wondering if there is a way to have a default image (pulled from the theme folder) called when a post doesn't have any image attachments.

    Any ideas?

  12. mores
    Member
    Posted 5 years ago #

    Since there are a number of parallel bits of code, which one are you using?
    You need to add an "else" somewhere. Using my own code:

    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 "<img src=DEFAULTIMAGE.JPG>";
    	}
    }

    That's the simplest solution.

  13. mores
    Member
    Posted 5 years ago #

    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.

    Try this:

    function postimage($size=medium) {
    	if ( sizeof($images = get_children(array(
    		'post_parent' => get_the_ID(),
    		'post_type' => 'attachment',
    		'numberposts' => 1,
    		'post_mime_type' => 'image',))) > X) /* X is number of desired images */
    	{
    		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 "<img src=DEFAULTIMAGE.JPG>";
    	}
    }

    Sorry, is untested. But it's something to do with the sizeof() thing, I'm not sure if it works right up where it is or if I can only get the size after I get the array.

  14. Nyo
    Member
    Posted 5 years ago #

    Thank you very much guys, you've saved my life ! :D
    Thank you!!!

  15. Indojepang
    Member
    Posted 5 years ago #

    Gee.. I'm glad I found this threat!! thank you.. up up up! this is really great!!

  16. Dunkkan
    Member
    Posted 5 years ago #

    I'm trying to set three posts like this (excerpts):

    thumb-----thumb-----thumb
    words-----words------words
    words-----words------words
    words-----words------words

    The code above only will show the first thumbnail for the first post.

    I use get_posts to set the multiple loops:

    <?php
     $posts = get_posts('numberposts=1&category=18');
     foreach($posts as $post) :
        setup_postdata($post);
     ?>
    <?php postimage('thumbnail'); ?>
    <?php the_excerpt(); ?>
    <?php endforeach; ?>

    In the second and third post I use an &offset=1 and &offset=2 respectively, which I add to the get_posts function.
    I've put as well the rewind_posts function at the begining of the template, so the three excerpts shows perfectly. But not the thumbs #2 and #3.

    I think I could fix it by arranging some arguments or arrays in the function.php, but I'm lost at this point. I tried to use the get_children, which requires once again some arguments I ignore.

    If anybody can provide any tip...

  17. tobydecks
    Member
    Posted 5 years ago #

    Hi,
    I'm a bit of a WP/PHP newbie. This post has been really helpful, but I'm a bit confused. I need to pull the images from each post individually, but instead of them linking to the actual image itself, I need the image to link to the post that its been pulled from. I've been using this bit of code.

    <?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);
    	   	}
    	   }
    
    	   ?>

    I guess I can use <?php the_ID(); ?> to link to the post, but don't know where it needs to go, or in fact what is generating the link to the image in the first place.

    Can anyone help?
    Have I made any sense?

    Cheers
    :)

  18. shaneholden
    Member
    Posted 5 years ago #

    I'm wanting to display a couple of random thumbnails from a specific category, with the thumbs linking to the post they are from. Pretty much like Matt does for his Serendipity.

    I'm trying out some of the code from this, but I'm needing random instead of latest.

    Any help?

    Thanks

  19. shaneholden
    Member
    Posted 5 years ago #

    Should I do a new topic since this one is marked resolved?

    Thanks in advance!

Topic Closed

This topic has been closed to new replies.

About this Topic