Wordpress: extract & resize post attachment
I know this question belongs in the wordpress forum, but I've posted about a half dozen questions there and have never gotten any responses - I like the community here much more, please don't move this question to wordpress.
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 $feat开发者_如何学Pythonure_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 attachment, resized to match the columns width. How can I do this? Thanks
The best way to do this would be to use featured images:
In your theme's functions.php file:
add_theme_support( 'post-thumbnails' ); // add theme support
add_image_size( 'home-feature', 100, 100, true ); // add an image size
I've set the image size to be 100x100 - but obviously you change this to suit your needs
See http://codex.wordpress.org/Function_Reference/add_theme_support and http://codex.wordpress.org/Function_Reference/add_image_size for more info on these
Then instead of <?php the_excerpt; ?>
you would have:
<?php if(has_post_thumbnail()) {
the_post_thumbnail( 'home-feature' );
} else {
// could show a standard image here - e.g. a grey box
} ?>
More info on this can be found here: http://codex.wordpress.org/Function_Reference/has_post_thumbnail and here: http://codex.wordpress.org/Function_Reference/the_post_thumbnail
UPDATE Sorry just spotted that you said attachments, an option would be something along the lines of:
$args = array(
'post_type' => 'attachment',
'numberposts' => -1, // bring them all
'post_status' => null,
'post_parent' => $post->ID, // post id with the gallery
'orderby' => 'menu_order',
'order' => 'ASC'
);
$attachments = get_posts($args);
if(count($attachments) > 0) {
foreach((array)$attachments as $attachment) {
?><li><img src="<?php print wp_get_attachment_url($img->ID); ?>" width="448" height="290" alt="<?php print $img->post_title;?>" title="<?php print $img->post_title;?>"></li><?php
}
}
Obviously in this foreach I've assumed all the attachments are images, but you could equally swap this out to just output the attachment
精彩评论