WordPress教程

WordPress 后台文章列表设置文章特色图片(缩略图)集成版

阿里云

设置文章特色图像对于 WordPress 用户应该都不陌生吧,就是文章的缩略图,目前几乎所有的国内 WordPress 主题,文章缩略图都是按照:特色图像》文章内第一张图片》默认图片,这个顺序进行显示。

但并不是每篇文章内的第一张图片都能作为缩略图进行完美显示,所以很多时候我们都需要单独设置文章特色图像,以达到最好的显示效果。

也想出现在这里?联系我们
创客主机

特别在新搭建 WordPress 网站的时候,往往可能需要频繁的设置文章特色图像,functions.php 添加以下代码:

  1. /**
  2.  * WordPress 后台文章列表设置文章特色图片(缩略图)集成版
  3.  * Plugin Name: Easy Thumbnail Switcher
  4.  */
  5. class doocii_Easy_Thumbnail_Switcher {
  6.  
  7.     public $add_new_str;
  8.     public $change_str;
  9.     public $remove_str;
  10.     public $upload_title;
  11.     public $upload_add;
  12.     public $confirm_str;
  13.  
  14.     public function __construct() {
  15.  
  16.         $this->add_new_str = __( '添加');
  17.         $this->change_str = __( '更改');
  18.         $this->remove_str = __( '移除');
  19.         $this->upload_title = __( '上传特色图片');
  20.         $this->upload_add = __( '确定');
  21.         $this->confirm_str = __( '你确定?');
  22.  
  23.         add_filter( 'manage_posts_columns', array( $this, 'add_column' ) );
  24.         add_action( 'manage_posts_custom_column', array( $this, 'thumb_column' ), 10, 2 );
  25.         add_action( 'admin_footer', array( $this, 'add_nonce' ) );
  26.         add_action( 'admin_enqueue_scripts', array( $this, 'scripts' ) );
  27.  
  28.         add_action( 'wp_ajax_ts_ets_update', array( $this, 'update' ) );
  29.         add_action( 'wp_ajax_ts_ets_remove', array( $this, 'remove' ) );
  30.  
  31.         add_image_size( 'ts-ets-thumb', 75, 75, array( 'center', 'center' ) );
  32.  
  33.     }
  34.  
  35.     /**
  36.      * 安全检查
  37.      */
  38.     public function add_nonce() {
  39.  
  40.         global $pagenow;
  41.  
  42.         if( $pagenow !== 'edit.php' ) {
  43.             return;
  44.         }
  45.         wp_nonce_field( 'ts_ets_nonce', 'ts_ets_nonce' );
  46.  
  47.     }
  48.  
  49.     /**
  50.      * 加载脚本
  51.      */
  52.     public function scripts( $pagenow ) {
  53.  
  54.         if( $pagenow !== 'edit.php' ) {
  55.             return;
  56.         }
  57.  
  58.         wp_enqueue_media();
  59.         wp_enqueue_script( 'doocii-ets-js', get_template_directory_uri() . '/js/script.js', array( 'jquery', 'media-upload', 'thickbox' ), '1.0', true );
  60.  
  61.         wp_localize_script( 'doocii-ets-js', 'ets_strings', array(
  62.             'upload_title' => $this->upload_title,
  63.             'upload_add' => $this->upload_add,
  64.             'confirm' => $this->confirm_str,
  65.         ) );
  66.  
  67.     }
  68.  
  69.     /**
  70.      * The action which is added to the post row actions
  71.      */
  72.     public function add_column( $columns ) {
  73.  
  74.         $columns['ts-ets-option'] = __( '缩略图');
  75.         return $columns;
  76.  
  77.     }
  78.  
  79.     /**
  80.      * 显示列
  81.      */
  82.     public function thumb_column( $column, $id ) {
  83.  
  84.         switch( $column ) {
  85.             case 'ts-ets-option':
  86.  
  87.                 if( has_post_thumbnail() ) {
  88.                     the_post_thumbnail( 'ts-ets-thumb' );
  89.                     echo '<br>';
  90.                     echo sprintf( '<button type="button" class="button-primary ts-ets-add" data-id="%s">%s</button>', esc_attr( $id ), $this->change_str );
  91.                     echo sprintf( ' <button type="button" class="button-secondary ts-ets-remove" data-id="%s">%s</button>', esc_attr( $id ), $this->remove_str );
  92.                 } else {
  93.                     echo sprintf( '<button type="button" class="button-primary ts-ets-add" data-id="%s">%s</button>', esc_attr( $id ), $this->add_new_str );
  94.                 }
  95.  
  96.                 break;
  97.         }
  98.  
  99.     }
  100.  
  101.     /**
  102.      * AJAX保存更新缩略图
  103.      */
  104.     public function update() {
  105.  
  106.         // 检查是否所有需要的数据都设置与否
  107.         if( !isset( $_POST['nonce'] ) || !isset( $_POST['post_id'] ) || !isset( $_POST['thumb_id'] ) ) {
  108.             wp_die();
  109.         }
  110.  
  111.         //验证
  112.         if( !wp_verify_nonce( $_POST['nonce'], 'ts_ets_nonce' ) ) {
  113.             wp_die();
  114.         }
  115.  
  116.         $id = $_POST['post_id'];
  117.         $thumb_id = $_POST['thumb_id'];
  118.  
  119.         set_post_thumbnail( $id, $thumb_id );
  120.  
  121.         echo wp_get_attachment_image( $thumb_id, 'ts-ets-thumb' );
  122.         echo '<br>';
  123.         echo sprintf( '<button type="button" class="button-primary ts-ets-add" data-id="%s">%s</button>', esc_attr( $id ), $this->change_str );
  124.         echo sprintf( ' <button type="button" class="button-secondary ts-ets-remove" data-id="%s">%s</button>', esc_attr( $id ), $this->remove_str );
  125.  
  126.         wp_die();
  127.  
  128.     }
  129.  
  130.     /**
  131.      * AJAX回调后删除缩略图
  132.      */
  133.     public function remove() {
  134.  
  135.         // Check if all required data are set or not
  136.         if( !isset( $_POST['nonce'] ) || !isset( $_POST['post_id'] ) ) {
  137.             wp_die();
  138.         }
  139.  
  140.         // Verify nonce
  141.         if( !wp_verify_nonce( $_POST['nonce'], 'ts_ets_nonce' ) ) {
  142.             wp_die();
  143.         }
  144.  
  145.         $id = $_POST['post_id'];
  146.  
  147.         delete_post_thumbnail( $id );
  148.  
  149.         echo sprintf( '<button type="button" class="button-primary ts-ets-add" data-id="%s">%s</button>', esc_attr( $id ), $this->add_new_str );
  150.  
  151.         wp_die();
  152.  
  153.     }
  154.  
  155. }
  156.  
  157. new doocii_Easy_Thumbnail_Switcher();

以下代码保存为 script.js,保存至 主题名/js 目录下

  1. (function($) {
  2.  
  3.     "use strict";
  4.  
  5.     if( typeof ts_ets === 'undefined' ) {
  6.         window.ts_ets = {};
  7.         ts_ets.upload_frame = false;
  8.     }
  9.  
  10.     $(document).on( 'click', 'button.ts-ets-remove', function() {
  11.  
  12.         ts_ets.tmp_id = $(this).data('id');
  13.         ts_ets.tmp_parent = $(this).closest('td.ts-ets-option');
  14.  
  15.         if( !confirm( ets_strings.confirm ) ) {
  16.             return;
  17.         }
  18.  
  19.         $.ajax({
  20.             url: ajaxurl,
  21.             method: "POST",
  22.             data: {
  23.                 action: 'ts_ets_remove',
  24.                 nonce: $('#ts_ets_nonce').val(),
  25.                 post_id: ts_ets.tmp_id
  26.             },
  27.             success: function( data ) {
  28.                 if( data != '' ) {
  29.                     ts_ets.tmp_parent.HTML( data );
  30.                 }
  31.             }
  32.         });
  33.  
  34.     });
  35.  
  36.     $(document).ready(function() {
  37.  
  38.         ts_ets.upload_frame = wp.media({
  39.             title: ets_strings.upload_title,
  40.             button: {
  41.                 text: ets_strings.upload_add,
  42.             },
  43.             multiple: false
  44.         });
  45.  
  46.         ts_ets.upload_frame.on( 'select', function() {
  47.  
  48.             ts_ets.selection = ts_ets.upload_frame.state().get('selection');
  49.  
  50.             ts_ets.selection.map( function( attachment ) {
  51.                 if( attachment.id ) {
  52.                     $.ajax({
  53.                         url: ajaxurl,
  54.                         method: "POST",
  55.                         data: {
  56.                             action: 'ts_ets_update',
  57.                             nonce: $('#ts_ets_nonce').val(),
  58.                             post_id: ts_ets.tmp_id,
  59.                             thumb_id: attachment.id
  60.                         },
  61.                         success: function( data ) {
  62.                             if( data != '' ) {
  63.                                 ts_ets.tmp_parent.html( data );
  64.                             }
  65.                         }
  66.                     });
  67.                 }
  68.             });
  69.  
  70.         });
  71.  
  72.     });
  73.  
  74.     $(document).on( 'click', 'button.ts-ets-add', function(e) {
  75.  
  76.         e.preventDefault();
  77.  
  78.         ts_ets.tmp_id = $(this).data('id');
  79.         ts_ets.tmp_parent = $(this).closest('td.ts-ets-option');
  80.  
  81.         if( ts_ets.upload_frame ) {
  82.             ts_ets.upload_frame.open();
  83.         }
  84.  
  85.     });
  86.  
  87. })(jQuery);

WordPress 后台文章列表设置文章特色图片(缩略图)集成版

已有 268 人购买
查看演示升级 VIP立刻购买

收藏
(0)

发表回复

热销模板

Ashade - 作品展示摄影相册WordPress汉化主题
LensNews

本站承接 WordPress / PbootCMS / DedeCMS 等
系统建站、仿站、开发、定制等业务!