条件语句-检查POST是否有附件图像

时间:2011-09-27 作者:jw60660

我正在处理一个条件语句(在循环中),该语句查找一个特征图像,如果没有,则获取一个默认图像,我已经计算出来了。但是,我还想找到一种方法,在使用默认图像之前先提取第一个图像(或任何图像)附件。无法找到正确的方法来检查附件图像,然后显示它。

我在函数中添加了一个特定的缩略图大小“特色拇指”。我的主题的php文件。如果我能在显示附件图像时利用它,那就太好了。

以下是我目前掌握的情况:

<?php $count=0; if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>

                        <div class="span-9 subfeature append-bottom <?php if(++$count%2==0) echo \'last\'?>">                                           

              <?php $thumb = get_the_post_thumbnail(); //Hack for Sitewide Tags Thumbnail process: assigns variable for use in conditional statement below ?>

              <?php if (has_post_thumbnail()) { 

             the_post_thumbnail(\'thumbnail\');

              } else

              if ( $thumb ) { //if not checks to see if there\'s a Sitewide Tags created thumbnail and uses that if there is

             the_post_thumbnail();

              } else {                    

               echo \'<img src="\'.get_bloginfo("stylesheet_directory").\'/images/maintenance/fallback-thumb.jpg" >\';
              }


              ?>             
非常感谢。

2 个回复
最合适的回答,由SO网友:Joost de Valk 整理而成

我在下面使用了这段代码,虽然有点多,但它基本上是通过帖子缩略图、附加图像、内容中的图像,然后循环到每个类别的图像(这是我网站上的固定设置,因此在get_post_category_image. 主要用途是通过最后一个函数echo get_post_image():

function get_post_content_thumbnail($num, $width=\'\', $height=\'\') {
    $content = get_the_content();
    $count = substr_count($content, \'<img\');
    $start = 0;
    for($i=1;$i<=$count;$i++) {
        $imgBeg = strpos($content, \'<img\', $start);
        $post = substr($content, $imgBeg);
        $imgEnd = strpos($post, \'>\');
        $postOutput = substr($post, 0, $imgEnd+1);
        $postOutput = preg_replace(\'/width="([0-9]*)" height="([0-9]*)"/\',
                                   \'\',
                                   $postOutput);
        $postOutput = str_replace(array(\'class="alignright"\', \'class="alignleft"\'), \'\', $postOutput);
        $postOutput = preg_replace(\'/title="([\\w-_]+)"/\',
                                   \'\',
                                   $postOutput);
        $image[$i] = $postOutput;
        $start=$imgEnd+1;
    }

    if(isset($image) && stristr($image[$num],\'<img\')) {
        return str_replace(\'<img \', \'<img \' . image_hwstring($width, $height), $image[$num]);
    }
}

function get_post_attachment_thumbnail($width=\'\', $height=\'\') {
    $files = get_children(\'post_parent=\'.get_the_ID().\'&post_type=attachment&post_mime_type=image\');
    if($files) {
        $keys = array_reverse(array_keys($files));
        $j=0;
        $attachment_id = $keys[$j];
        $image = image_downsize($attachment_id, \'thumbnail\');
        $image[0] = str_replace(\'http://yoast.com\', \'http://cdn.yoast.com\', $image[0]);
        return \'<img src="\' . $image[0] . \'" alt="" \' . image_hwstring($width, $height) . \'/>\';
    }
}

function get_post_category_image($p_id=null, $width=\'\', $height=\'\'){
    global $post_id, $top_category;
    if (!$p_id)
        $p_id = $post_id;

    if ( isset($top_category) )
        return $top_category;

    $post_categories = get_the_category( $p_id );

    $slug = count($post_categories) ? $post_categories[0]->slug : \'\';

    if ($slug && file_exists(TEMPLATEPATH . \'/img/post-img/\'.$slug.\'.png\')){
        return \'<img \' . image_hwstring($width, $height). \' src="\' . get_bloginfo(\'template_url\') . \'/img/post-img/\'.$slug.\'.png" alt="\'.$post_categories[0]->cat_name.\'"/>\';
    } else {
        return \'<img \' . image_hwstring($width, $height). \' src="\' . get_bloginfo(\'template_url\') . \'/img/post-img/default.jpg" alt=""/>\';
    }
}

function get_post_image($string_size=\'post-thumbnail\', $width=0){
    if ( has_post_thumbnail() ) :
        return get_the_post_thumbnail(NULL, $string_size, array(\'title\'=>\'\'));
    elseif( $attached_image = get_post_attachment_thumbnail($width)):
        return $attached_image;
    elseif( $post_content_thumbnail = get_post_content_thumbnail(1, $width)):
        return $post_content_thumbnail;
    else:
        return get_post_category_image(NULL, $width);
    endif;
}

SO网友:Alexey

我正在使用这个代码

   <?php
    $args = array( 
        \'post_type\' => \'attachment\', 
        \'numberposts\' => -1, 
        \'post_mime_type\' => \'image\',
        \'post_status\' => null, 
        \'post_parent\' => get_the_ID(), 
        \'orderby\' => \'menu_order\', 
        \'order\' => \'ASC\',
    );
    $attachments = get_posts($args);
    if ($attachments):
            foreach($attachments as $key => $image):

                $attached_image = wp_get_attachment_metadata ( $image->ID);
                $upload_dir = wp_upload_dir();


        ?>

                    <div class="thumb <?php echo ($attached_image[\'sizes\'][\'thumbnail\'][\'width\'] == "200") ? "hor" : "ver"; ?>">
                        <a href="<?php echo $upload_dir[\'baseurl\'] . "/" . $attached_image[\'file\']; ?>" rel="colorbox"><img src="<?php echo $upload_dir[\'baseurl\'] . "/" . $attached_image[\'sizes\'][\'thumbnail\'][\'file\']; ?>" alt="" width="<?php echo $attached_image[\'sizes\'][\'thumbnail\'][\'width\'] * .6; ?>" height="<?php echo $attached_image[\'sizes\'][\'thumbnail\'][\'height\'] * .6; ?>" /></a>
                    </div>


        <?php endforeach; endif; ?>

结束

相关推荐

用于WordPress后端的Nginx或Cherokee(+php-fpm)?

多年来,我一直在使用apache2和mod\\u php,但最近我迁移到nginx 使用php fpm。虽然速度更快、更稳定,但我也看了一下cherokee, 因为两者都可以处理php fpm和tons\'o\'访问者。作为WordPress主机,哪个服务器更好,有人对此进行过比较吗?更新Rarst要求问题更具体一些,因此目标(按顺序):速度、稳定性、安全性、可预测的内存使用