如果未设置特色图像,则显示占位符/默认img

时间:2012-03-02 作者:Vince Pettit

如果没有可用的特征图像,我需要修改以下代码以显示占位符。

我知道我需要添加if 语句,但不确定我需要的确切编码。很明显,在简单的英语中,如果有缩略图,就显示它,如果没有显示占位符。

<?php $rel = $related->show(get_the_ID(), true);
     $count = 1;
    foreach ($rel as $r) {
     $class= ($count%6 == 0)?"category-related-item-right":"";

     echo \'<div class="category-related-item \'.$class.\'"><a href=\'.get_permalink($r->ID).\'>\'.\'<div class=category-related-title>\'.$r->post_title.\'</div>\'.get_the_post_thumbnail($r->ID, array(50,50)).\'</a></div>\';
$count++;
}?>
我尝试了以下方法,但有些地方不太正确,因为它正在破坏页面。。。

<?php $rel = $related->show(get_the_ID(), true);
$count = 1;
foreach ($rel as $r) {
$class= ($count%6 == 0)?"category-related-item-right":"";

echo \'<div class="category-related-item \'.$class.\'"><a href=\'.get_permalink($r->ID).\'>\'.\'<div class=category-related-title>\'.$r->post_title.\'</div>\';
if (get_the_post_thumnail($r->ID)) : 
get_the_post_thumbnail($r->ID, array(50,50));
else :
echo \'<img src="image_url"/>\';
endif;
echo \'</a></div>\';
$count++;
}?>

4 个回复
最合适的回答,由SO网友:Stephen Harris 整理而成

的拼写错误thumbnail (thumnail)在if语句中。

而且get_the_post_thumbnail 会回显缩略图,但只返回html。你需要回应它。

此外,要检查帖子是否有缩略图,可以使用has_post_thumbnail.

 if ( has_post_thumbnail($r->ID)) {
    echo get_the_post_thumbnail($r->ID, array(50,50));
 }else{
    echo \'<img src="image_url"/>\';
 }

SO网友:Bipin Sapkota

对于知识较少或一无所知的设计师,以及wordpress中的状态者,这里给出了获取图像代码的方法。设计师只需复制wordpress获取图像代码并粘贴即可。

if ( has_post_thumbnail($post->ID) ){   
    $image = wp_get_attachment_image_src(get_post_thumbnail_id(), \'full\');
    $image = $image[0];
    echo \'<div id="product" class="MagicZoomPlus" href="\'.$image.\'" rel="selectors-class: Active"><img src="\'.$image.\'" alt="" /></div>\';
}else{  
    $image = get_template_directory_uri() .\'/img/placeholder-580.png\'; 
    echo \'<a id="product" class="MagicZoomPlus" href="\'.$image.\'" rel="selectors-class: Active"><img src="\'.$image.\'" alt="" /></a>\';
}

SO网友:kaiser

为主题添加默认图像的功能也可以在Trac上找到以下代码作为票证1)

请将您自己添加为@cc以支持它。谢谢

Ticket Link

使用函数wp_default_img():

它使用属性的输入数组,并提供两个过滤器(wp\\u default\\u img\\u attr&wp\\u default\\u img)。因此,设置默认图像就像使用过滤器一样简单(如果主题开发人员对默认参数的功能不满意),最后只需添加&hellip;

// functions.php during init:
add_image_size( \'default_img\', 80, 80, true );

// Inside some template
$placeholder = get_site_url( null, \'your_path\' ).\'/some_img.jpg\';
echo wp_default_img( array( \'url\' => $placeholder, \'size\' => \'default_img\' ) );
当使用add\\u image\\u size();;注册大小时,如果第4个参数设置为true,该函数还关心裁剪图像;。

function wp_default_img( $attr )
{
        // Sizes registered via add_image_size();
        global $_wp_additional_image_sizes;

        $defaults = array(
             \'size\'     => \'medium\'
            ,\'classes\'  => false
            ,\'alt\'      => false
            ,\'title\'    => false
            ,\'align\'    => \'none\'
            ,\'echo\'     => true 
        );

        $attr = wp_parse_args( $attr, $defaults );

        if ( \'thumb\' === $attr[\'size\'] )
            $attr[\'size\'] = \'thumbnail\';

        // Size in built in sizes - call size setting from DB
        # behavoir in here, dependent on outcome of @link http://core.trac.wordpress.org/ticket/18947
        if ( ! in_array( $attr[\'size\'], array_keys( $_wp_additional_image_sizes ) ) )
        {
            $sizes                        = get_intermediate_image_sizes();
            // Get option - gladly autoloaded/can use wp_cache_get();
            $size_data[\'width\']  = intval( get_option( "{$attr[\'size\']}_size_w") );
            $size_data[\'height\']= intval( get_option( "{$attr[\'size\']}_size_h") );
            // Not sure how this will behave if cropped is false (autoloaded option not added)
            $size_data[\'crop\']    = get_option( "{$attr[\'size\']}_crop" ) ? get_option( "{$attr[\'size\']}_crop" ) : false;
        }
        // Size array from global registered additional/custom sizes array
        else 
        {
            $size_data = $_wp_additional_image_sizes[ $attr[\'size\'] ];
        }

        // Retrieve image width & height
        $img_info       = @getimagesize( $attr[\'url\'] );

        // Calculate final dimensions - if "crop" was set to true during add_image_size(), the img will get ... cropped
        $end_sizes      = image_resize_dimensions( $img_info[0], $img_info[1], $size_data[\'width\'], $size_data[\'height\'], $size_data[\'crop\'] );

        // defaults to px units - can\'t get changed, as applying units is not possible
        $hwstring       = \' \'.trim( image_hwstring( $end_sizes[4], $end_sizes[5] ) );

        // Attributes:
        // Not made required as users tend to do funky things (...and lock screen readers out)
        $attr[\'alt\'] = $attr[\'alt\'] ? \' alt="\'.esc_attr( $attr[\'alt\'] ).\'"\' : \'\';

        if ( ! $attr[\'title\'] )
        {
            $mime = explode( "/", $img_info[\'mime\'] );
            $attr[\'title\'] = sprintf( __(\'default image of type: %1$s\'), ucfirst( $mime[1] ) );
        }
        $attr[\'title\'] = $attr[\'title\'] ? \' title="\'.esc_attr( $attr[\'title\'] ).\'"\' : \'\';

        $attr[\'classes\'] = "wp-img-default ".esc_attr( $attr[\'classes\'] ? $attr[\'classes\'] : \'\' );
        $attr[\'align\'] = $attr[\'align\'] ? "align".esc_attr( $attr[\'align\'] ) : \'\';
        $attr[\'size\'] = "size-".esc_attr( $attr[\'size\'] );

        // Allow filtering of the default attributes
        $attributes  = apply_filters( \'wp_default_img_attr\', $attr );

        // Build class attribute, considering that maybe some attribute was unset via the filter
        $classes  = \' class="\';
        $classes .= \'wp-img-default\'.esc_attr( $attr[\'classes\'] ? \' \'.$attr[\'classes\'] : \'\' );
        $classes .= $attr[\'align\'] ? \' \'.esc_attr( $attr[\'align\'] ) : \'\';
        $classes .= $attr[\'size\'] ? \' \'.esc_attr( $attr[\'size\'] ).\'" \' : \'" \';

        $url        = trim( $attr[\'url\'] );
        $image      = "<img src=\'{$url}\'{$hwstring}{$classes}{$attr[\'alt\']}{$attr[\'title\']} />";

        // Allow filtering of output
        $image      = apply_filters( \'wp_default_img\', $image );

        if ( ! $attr[\'echo\'] )
            return $image;

        return print $image;
}

SO网友:Kevin Muthwa
function before_imageless_product() {
    if( !has_post_thumbnail( get_the_id() ) ){
        remove_action( \'woocommerce_before_shop_loop_item_title\', \'woocommerce_template_loop_product_thumbnail\', 10 );
        echo \'<div class="no-image">\';
    }
}
add_action( \'woocommerce_before_shop_loop_item\', \'before_imageless_product\', 9 );
结束

相关推荐

Images don't show up

我刚刚安装了一个新的多站点安装。一切都像我一样工作,只是图像没有显示在任何子网站上。Any idea why the images don\'t show up?我正在共享Windows服务器(由Arvixe.com托管)中的IIS6上运行Wordpress 3.1.1。辅助站点位于子目录而不是子域中。主站点的图像与标题图像一样有效,但子站点中的图像不会显示。我知道这些图片在博客中出现时,正在正确上传。服务器上的dir文件夹。如果我这样导航它们,它们也会出现:http://www.example.com/