【问题标题】:Wordpress: extract & resize post attachmentWordpress:提取和调整帖子附件的大小
【发布时间】:2011-08-03 22:33:42
【问题描述】:

我知道这个问题属于 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>

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

【问题讨论】:

    标签: php wordpress customization


    【解决方案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 了解更多信息

    那么你会得到:而不是&lt;?php the_excerpt; ?&gt;

    <?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 中,我假设所有附件都是图像,但您同样可以将其换掉以仅输出附件

    【讨论】:

      猜你喜欢
      • 2015-05-25
      • 2016-11-18
      • 2013-01-20
      • 1970-01-01
      • 1970-01-01
      • 2014-04-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多