Wordpress:提取&调整帖子附件的大小

时间:2011-08-03 22:33:43

标签: php wordpress customization

我知道这个问题属于wordpress论坛,但我在那里发布了大约六个问题并且从未得到任何回复 - 我更喜欢这里的社区,请不要将这个问题转移到wordpress。

我正在寻找一种方法来提取类别最新帖子的附件(可能是照片,youtube vid或soundcloud歌曲),调整大小,然后将其显示在我的主页上。查看我的网站http://beachief.com/,看看我的意思 - “每日长官”和“部落音乐”部分是我想要这样做的地方。我使用以下代码成功调整了这两个部分上方的图像:

<img src="<?php bloginfo('template_url'); ?>/images/philadelphia.jpg" alt="Featured picture" class="scaled"/>

但这仅适用于我有精确文件位置的图像。这是我用来获取最新帖子并显示其中一些内容的代码:

<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>

我想摆脱摘录并显示帖子附件,调整大小以匹配列宽。我怎样才能做到这一点?感谢

1 个答案:

答案 0 :(得分:-1)

最好的方法是使用精选图片:

在主题的functions.php文件中:

add_theme_support( 'post-thumbnails' ); // add theme support
add_image_size( 'home-feature', 100, 100, true ); // add an image size 

我已将图片大小设置为100x100 - 但显然您可以根据需要更改此图片

有关这些

的更多信息,请参阅http://codex.wordpress.org/Function_Reference/add_theme_supporthttp://codex.wordpress.org/Function_Reference/add_image_size

然后代替<?php the_excerpt; ?>

<?php if(has_post_thumbnail()) {
    the_post_thumbnail( 'home-feature' );
} else {
    // could show a standard image here - e.g. a grey box
} ?>

有关此内容的更多信息,请访问:http://codex.wordpress.org/Function_Reference/has_post_thumbnail,此处:http://codex.wordpress.org/Function_Reference/the_post_thumbnail

更新 对不起只是发现你说附件,一个选项将是:

$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
    }
}

显然在这个foreach中我假设所有的附件都是图像,但你可以同样交换出来只输出附件