Rui

关闭缩略图功能和调整Wpjam插件
虽然本博客是Typecho的,但是还运营着一个wordpress的,也用着水煮鱼的七牛插件。 新的Wpjam上来直...
扫描右侧二维码阅读全文
02
2019/03

关闭缩略图功能和调整Wpjam插件

虽然本博客是Typecho的,但是还运营着一个wordpress的,也用着水煮鱼的七牛插件。

新的Wpjam上来直接把缩略图功能给毙了,如有还想用这个插件的CDN功能,就要自己修改主题,用七牛的自动裁剪功能,但是又要修改主题,所以有点麻烦。

主题-functions.php文件下,注释掉一些内容,主要是去掉主题自行裁剪缩略图的功能。

add_theme_support( 'post-thumbnails' );

// Post thumbnails
//set_post_thumbnail_size( 820, 312, true );
// Hero Image on the front page template
//add_image_size( 'sela-hero-thumbnail', 1180, 610, true );
// Full width and grid page template
//add_image_size( 'sela-page-thumbnail', 1180, 435, true );
// Grid child page thumbnail
//add_image_size( 'sela-grid-thumbnail', 360, 242, true );
// Testimonial thumbnail
//add_image_size( 'sela-testimonial-thumbnail', 90, 90, true );

wpjam-basicadminhookscustom.php,注释一下内容:(在最后)

Wpjam直接把所有学缩略图功能全部给屏蔽了,这就导致要自己去后台替换主题的插件,不免有些太暴力,所以把这个功能关闭。
add_action('wp_loaded', function (){
    if(CDN_NAME == '')
        return;

    add_filter('pre_option_thumbnail_size_w',    '__return_zero');
    add_filter('pre_option_thumbnail_size_h',    '__return_zero');
    add_filter('pre_option_medium_size_w',        '__return_zero');
    add_filter('pre_option_medium_size_h',        '__return_zero');
    add_filter('pre_option_large_size_w',        '__return_zero');
    add_filter('pre_option_large_size_h',        '__return_zero');

    add_filter('intermediate_image_sizes_advanced', function($sizes){
        if(isset($sizes['full'])){
            return ['full'=>$sizes['full']];
        }else{
            return [];
        }
    });

    add_filter('image_size_names_choose', function($sizes){
        if(isset($sizes['full'])){
            return ['full'=>$sizes['full']];
        }else{
            return [];
        }
    });

    add_filter('upload_dir', function($uploads){
        $uploads['url']        = wpjam_get_thumbnail($uploads['url']);
        $uploads['baseurl']    = wpjam_get_thumbnail($uploads['baseurl']);
        return $uploads;
    });

    add_filter('wp_calculate_image_srcset_meta', '__return_empty_array');

    // add_filter('image_downsize', '__return_true');
    add_filter('wp_get_attachment_image_src', function($image, $attachment_id, $size, $icon){
        return  wpjam_get_attachment_image_src($attachment_id, $size);
    }, 10 ,4);

    add_filter('wp_prepare_attachment_for_js', function($response, $attachment, $meta){
        if(isset($response['sizes'])){
            $orientation    = $response['sizes']['full']['orientation'];

            foreach (array('thumbnail', 'medium', 'medium_large', 'large') as $s) {
                $image_src = wpjam_get_attachment_image_src($attachment->ID, $s);

                $response['sizes'][$s]    = array(
                    'url'            => $image_src[0],
                    'width'            => $image_src[1],
                    'height'        => $image_src[2],
                    'orientation'    => $orientation
                );
            }
        }

        return $response;
    }, 10, 3);
}, 11);

另外这次把媒体菜单都给影藏了,wpjam-basicadminhooksadmin-menus.php中注释掉一下部分找回:
if(CDN_NAME){
          remove_submenu_page('options-general.php', 'options-media.php');
      }

还有一个问题,就是作者为了考虑高清屏幕,硬性设置了两倍的缩略图比例。

在wpjam-basic/public/wpjam-cdn.php中两处$retina=2改为$retina=1

function wpjam_post_thumbnail($size='thumbnail', $crop=1, $class='wp-post-image', <strong>$retina=1</strong>){
    echo wpjam_get_post_thumbnail(null, $size, $crop, $class, $retina);
}

function wpjam_get_post_thumbnail($post=null, $size='thumbnail', $crop=1, $class='wp-post-image', <strong>$retina=1</strong>){
    if($post_thumbnail_url = wpjam_get_post_thumbnail_url($post, $size, $crop, $retina)){
        return '<img src="'.$post_thumbnail_url.'" alt="'.the_title_attribute(['echo'=>false]).'" class="'.$class.'"'.wpjam_image_hwstring($size).' />';
    }else{
        return '';
    }
}

之后就是修改主题,主题根据情况有很多地方需要修改,其实就替换有关thumbnail的函数。

wpjam_post_thumbnail(array(750,400), $crop=1)替换the_post_thumbnail()

wpjam_get_post_thumbnail($post,array(750,400), $crop=1)替换get_the_post_thumbnail()

<figure class="blog-post-cover">
            <?php is_single()    
                //? the_post_thumbnail()
                ?wpjam_post_thumbnail(array(750,400), $crop=1)
                //: printf( '<a href="%s">%s</a>', get_the_permalink(), get_the_post_thumbnail() ); 
                 : printf( '<a href="%s">%s</a>', get_the_permalink(), wpjam_get_post_thumbnail($post,array(750,400), $crop=1)); ?>
        </figure>
Last modification:March 2nd, 2019 at 03:48 pm

One comment

  1. David

    wp越来越臃肿,已经不怎么用,特别是这个插件,简直流氓!

Leave a Comment