Default thumb - how to set it

时间:2013-09-03 作者:Max

我需要的代码将添加默认的拇指,如果没有特色的图像可用。我使用了默认的thumb plus,但是这个插件使用了大量资源,所以我需要删除它。

我正试着跟踪这个tutorial

但运气不好。在我的小部件中,我有以下代码

<?php if (  (function_exists(\'has_post_thumbnail\')) && (has_post_thumbnail())  ) { ?>
已编辑:和您可以在此处看到的全部代码pastebin。com/715evL1E

你知道怎么做吗?谢谢

我也尝试使用建议的代码,但出现了一个错误。这是我在索引中最后尝试的内容。php

<?php if (  (function_exists(\'has_post_thumbnail\')) && (has_post_thumbnail())  ) { ?>


                <a href="<?php the_permalink(); ?>" rel="bookmark" title="<?php the_title(); ?>"><?php the_post_thumbnail(\'large-thumb\'); } ?>
  <img src="<?php echo get_template_directory_uri(); ?>/images/transfer-deadline-150x150.jpg" /><?php } ?></a>

3 个回复
SO网友:kaiser

虽然核心中没有内置此类功能,但Track Ticket 我两年前求婚了。您可以简单地从建议的wp_default_img() 作用我将非常感谢大家对这张票的支持,因为这对于每个主题开发人员来说都是非常需要的。提前谢谢。

MU Plugin

以下代码将被放入~/wp-content/mu-plugins 文件夹(或任何您命名的文件夹)。默认情况下,它将在网络安装的每个站点中可用,或者在单个站点安装的任何地方都可用。使用方法如下:

default_img( array( /* attributes - see doc block */ ) );
只需将下面的代码以任何名称保存到新文件中,您就可以开始了。

<?php
defined( \'ABSPATH\' ) OR exit;
/**
 * Plugin Name: Default Image template tag
 * Plugin URI: http://unserkaiser.com
 * Description: Adds a default image template tag
 * Version: 0.1
 * Author: Franz Josef Kaiser
 * Author URI: http://unserkaiser.com
 * License: GNU GPL 2 <https://gist.github.com/1365159>
 */

/**
 * Default image
 *
 * Builds an default <img> for use in themes or plugins before any other images are added.
 * Resizes & crops the image using the built-in (retireved via `get_intermediate_image_sizes();`)
 * or custom image (added via `add_image_size();`) sizes.
 *
 * Retrieves calculated resize dimension @uses image_resize_dimensions();
 * Builds the width and height string @uses image_hwstring();
 *
 * @param $attr
 * @internal param $args (array)
 *        string $url URl to the given default image.
 *        string $size Optional. Default is \'medium\'.
 *        string (optional) $alt Image Description for the alt attribute.
 *        string (optional) $title Image Description for the title attribute.
 *        string (optional) $align Part of the class name for aligning the image.
 *        string (optional) $echo Wheter to return or echo the $image
 * @return string HTML IMG element for given image attachment
 */
function default_img( $attr )
{
    // Sizes registered via add_image_size();
    global $_wp_additional_image_sizes;

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

    $attr = wp_parse_args( $attr, $defaults );
    $attr = array_map( \'esc_attr\', $attr );

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

    // Size in built in sizes - call size setting from DB
    # behavoir in here dependent on @link http://core.trac.wordpress.org/ticket/18947
    # if in core, we change to:
    # $sizes = get_intermediate_image_sizes();
    # $size_data = $sizes[ $size ];
    if ( ! in_array(
        $attr[\'size\'],
        array_keys( $_wp_additional_image_sizes )
        ) )
    {
        # @TODO delete?
        $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)
        if ( ! $size_data[\'crop\'] = get_option( "{$attr[\'size\']}_crop" ) )
            $attr[\'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)
    ! empty( $attr[\'alt\'] ) AND $attr[\'alt\'] = " alt=\'{$attr[\'alt\']}\'";

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

    $attr[\'title\'] = " title=\'{$attr[\'title\']}\'";

    $attr[\'align\'] = " align{$attr[\'align\']}";
    $attr[\'size\']  = " size-{$attr[\'size\']}";

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

    // Build class attribute, considering that maybe some attribute was unset via the filter
    $classes  = "class=\'wp-img-default{$attr[\'align\']}{$attr[\'classes\']}{$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 );

    $attr[\'echo\'] AND print $image;

    return $image;
}

SO网友:Chip Bennett

WordPress core中没有这样的功能。如果没有选择特色图像,则必须创建、上载并调用自己的默认缩略图。例如,如果主题文件结构中有默认缩略图,则可以使用has_post_thumbnail() 检查当前帖子是否有缩略图,以及the_post_thumbnail() 显示它;必须在内部使用这两个功能the loop.

   if ( has_post_thumbnail() ) {
      the_post_thumbnail();
   } else {
   ?>
      <img src="<?php echo get_template_directory_uri(); ?>/images/default-thumb.jpg" />
   <?php
   }
?>
有很多方法可以实现;这只是一个又快又脏的例子。

SO网友:cybmeta

has\\u post\\u thumbnail()函数used without the ID 必须在里面the loop.

while(has_post()) {
  the_post();
  if ( has_post_thumbnail() ) {
     the_post_thumbnail();
  } else {
    ?>
    <img src="<?php echo get_template_directory_uri(); ?>/images/default-thumb.jpg" />
  <?php
  }
}
还可以将has\\u post\\u thumbnail()与特定的帖子id一起使用:

 <?php
     //Just an exampl
     $post_id = 45;
     if ( has_post_thumbnail($post_id) ) {
        //in this case we can not use the_post_thumbnail
         get_the_post_thumbnail($post_id, \'thumbnail\');
     } else {
     ?>
        <img src="<?php echo get_template_directory_uri(); ?>/images/default-thumb.jpg" />
     <?php
     }
此外,请确保主题支持在函数中添加此项的后期缩略图。php

add_theme_support( \'post-thumbnails\' ); 
如果您发布小部件的完整代码,我可以教您如何实现默认缩略图功能。

结束

相关推荐