WordPress.org

Ready to get started?Download WordPress

Forums

Extract and resize a post attachment (5 posts)

  1. Cbas23
    Member
    Posted 2 years ago #

    I'm looking for a way to extract the attachment (could be a photo, youtube vid, or soundcloud song) of a category's most recent post, resize it, and then display it on my homepage. Check out my site at http://beachief.com/ to see what I mean - the "Daily Chief" and "Tribal Music" sections are where I want to do this. I successfully resized the images above those two sections with this code:
    <img src="<?php bloginfo('template_url'); ?>/images/philadelphia.jpg" alt="Featured picture" class="scaled"/>
    But that only works for images that I have an exact file location to. This is the code I'm using to get my most recent posts and display some of their contents:

    <ul class="link-item"> <?php $feature_post = get_posts( 'category=4&numberposts=1' ); ?>
    <?php foreach( $feature_post as $post ) : setup_postdata( $post ); ?>
    <li><h2 class="link-item"><?php the_category(' '); ?></h2></li>
    <?php endforeach; ?>
    <?php $feature_post = get_posts( 'category=4&numberposts=1' ); ?>
    <?php foreach( $feature_post as $post ) : setup_postdata( $post ); ?>
       <li class="list-time"><?php the_time('d'); ?>.<?php the_time('M'); ?></li>
       <li class="list-title"><a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a></li>
       <li class="link-item"><?php the_excerpt(); ?></li>
    <?php endforeach; ?> </ul>

    I want to get rid of the excerpt and display the posts first attachment, resized to match the columns width. How can I do this? Thanks

  2. digibucc
    Member
    Posted 2 years ago #

    the one question that comes up for me is - how do you plan on selecting
    a specific attachment without the url? you can use the resource id.

    you'd get attachment ids for the post(by post slug or id), then use the attachment id to find the filename.

    one way to get the ids, though i thought there was an easier one - you can use get_children() with the parent id in it's arg array. then you'd
    need to choose which attachment. this page has examples.

    then you could use wp_get_attachment_url to return a url that can be used in your existing code.

    good luck

  3. Cbas23
    Member
    Posted 2 years ago #

    Thanks for the response. The wp_get_attachment_url doesn't seem to work though. I got the ID for the first image in the post and tried to display it with this code:

    <?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");
    ?>
    <a href="http://beachief.com/?cat=10"><img src="<?php wp_get_attachment_url($attachment_id); ?>" alt="Daily Chief" class="scaled"/></a>

    but all I get is a blank image box. What's wrong?

  4. Cbas23
    Member
    Posted 2 years ago #

    Actually I got it. Just needed to add "echo" before the call. This code works:

    <?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");
    ?>
    <a href="http://beachief.com/?cat=10"><img src="<?php echo wp_get_attachment_url($attachment_id); ?>" alt="Daily Chief" class="scaled"/></a>

    It gets the first image attached to the post, resizes it as needed (parameters set in the "scaled" css class), and adds a hyperlink to wherever you want.

    Now.. How could I do the same thing for a Youtube video embedded in a post?

  5. digibucc
    Member
    Posted 2 years ago #

    you'd have to pull post content instead of attachments. embedded is embedded, you can't pull it out of your post as it doesn't exist there, the best you can do is have it replicate the code it uses in
    the post to display it elsewhere.

    making one area display both images and videos would be a bit of work.

Topic Closed

This topic has been closed to new replies.

About this Topic