- 资源介绍
- 更新记录
- 安装教程
目前很多 wordpress 主题都具有缩略图功能,但你想没想过后台文章列表也可以显示缩略图,貌似之前有个插件可以实现这一功能,不过名称忘了,所以柠檬今天给大家推荐两个简单的方案。
方法一
就是在您的 wordpress 后台文章列表里面的右侧可以显示出当时有设置特色图片文章的图片缩略图,很实力的一个小的增强功能,可以更方法的将文章封面展示在列表里,免除了打开内容或是跳转前端确认文章封面特色图片的步骤。找到我们主题的 functions.php 文件在里面添加以下代码:
/**
* WordPress 后台文章列表设置文章特色图片(缩略图)集成版
* Plugin Name: Easy Thumbnail Switcher
*/
class doocii_Easy_Thumbnail_Switcher {
public $add_new_str;
public $change_str;
public $remove_str;
public $upload_title;
public $upload_add;
public $confirm_str;
public function __construct() {
$this->add_new_str = __( \'添加\');
$this->change_str = __( \'更改\');
$this->remove_str = __( \'移除\');
$this->upload_title = __( \'上传特色图片\');
$this->upload_add = __( \'确定\');
$this->confirm_str = __( \'你确定?\');
add_filter( \'manage_posts_columns\', array( $this, \'add_column\' ) );
add_action( \'manage_posts_custom_column\', array( $this, \'thumb_column\' ), 10, 2 );
add_action( \'admin_footer\', array( $this, \'add_nonce\' ) );
add_action( \'admin_enqueue_scripts\', array( $this, \'scripts\' ) );
add_action( \'wp_ajax_ts_ets_update\', array( $this, \'update\' ) );
add_action( \'wp_ajax_ts_ets_remove\', array( $this, \'remove\' ) );
add_image_size( \'ts-ets-thumb\', 75, 75, array( \'center\', \'center\' ) );
}
/**
* 安全检查
*/
public function add_nonce() {
global $pagenow;
if( $pagenow !== \'edit.php\' ) {
return;
}
wp_nonce_field( \'ts_ets_nonce\', \'ts_ets_nonce\' );
}
/**
* 加载脚本
*/
public function scripts( $pagenow ) {
if( $pagenow !== \'edit.php\' ) {
return;
}
wp_enqueue_media();
wp_enqueue_script( \'doocii-ets-js\', get_template_directory_uri() . \'/js/script.js\', array( \'jquery\', \'media-upload\', \'thickbox\' ), \'1.0\', true );
wp_localize_script( \'doocii-ets-js\', \'ets_strings\', array(
\'upload_title\' => $this->upload_title,
\'upload_add\' => $this->upload_add,
\'confirm\' => $this->confirm_str,
) );
}
/**
* The action which is added to the post row actions
*/
public function add_column( $columns ) {
$columns[\'ts-ets-option\'] = __( \'缩略图\');
return $columns;
}
/**
* 显示列
*/
public function thumb_column( $column, $id ) {
switch( $column ) {
case \'ts-ets-option\':
if( has_post_thumbnail() ) {
the_post_thumbnail( \'ts-ets-thumb\' );
echo \'<br>\';
echo sprintf( \'<button type=\"button\" class=\"button-primary ts-ets-add\" data-id=\"%s\">%s</button>\', esc_attr( $id ), $this->change_str );
echo sprintf( \' <button type=\"button\" class=\"button-secondary ts-ets-remove\" data-id=\"%s\">%s</button>\', esc_attr( $id ), $this->remove_str );
} else {
echo sprintf( \'<button type=\"button\" class=\"button-primary ts-ets-add\" data-id=\"%s\">%s</button>\', esc_attr( $id ), $this->add_new_str );
}
break;
}
}
/**
* AJAX保存更新缩略图
*/
public function update() {
// 检查是否所有需要的数据都设置与否
if( !isset( $_POST[\'nonce\'] ) || !isset( $_POST[\'post_id\'] ) || !isset( $_POST[\'thumb_id\'] ) ) {
wp_die();
}
//验证
if( !wp_verify_nonce( $_POST[\'nonce\'], \'ts_ets_nonce\' ) ) {
wp_die();
}
$id = $_POST[\'post_id\'];
$thumb_id = $_POST[\'thumb_id\'];
set_post_thumbnail( $id, $thumb_id );
echo wp_get_attachment_image( $thumb_id, \'ts-ets-thumb\' );
echo \'<br>\';
echo sprintf( \'<button type=\"button\" class=\"button-primary ts-ets-add\" data-id=\"%s\">%s</button>\', esc_attr( $id ), $this->change_str );
echo sprintf( \' <button type=\"button\" class=\"button-secondary ts-ets-remove\" data-id=\"%s\">%s</button>\', esc_attr( $id ), $this->remove_str );
wp_die();
}
/**
* AJAX回调后删除缩略图
*/
public function remove() {
// Check if all required data are set or not
if( !isset( $_POST[\'nonce\'] ) || !isset( $_POST[\'post_id\'] ) ) {
wp_die();
}
// Verify nonce
if( !wp_verify_nonce( $_POST[\'nonce\'], \'ts_ets_nonce\' ) ) {
wp_die();
}
$id = $_POST[\'post_id\'];
delete_post_thumbnail( $id );
echo sprintf( \'<button type=\"button\" class=\"button-primary ts-ets-add\" data-id=\"%s\">%s</button>\', esc_attr( $id ), $this->add_new_str );
wp_die();
}
}
new doocii_Easy_Thumbnail_Switcher();
以下代码保存为 script.js,保存至 主题名/js 目录下:
(function($) {
\"use strict\";
if( typeof ts_ets === \'undefined\' ) {
window.ts_ets = {};
ts_ets.upload_frame = false;
}
$(document).on( \'click\', \'button.ts-ets-remove\', function() {
ts_ets.tmp_id = $(this).data(\'id\');
ts_ets.tmp_parent = $(this).closest(\'td.ts-ets-option\');
if( !confirm( ets_strings.confirm ) ) {
return;
}
$.ajax({
url: ajaxurl,
method: \"POST\",
data: {
action: \'ts_ets_remove\',
nonce: $(\'#ts_ets_nonce\').val(),
post_id: ts_ets.tmp_id
},
success: function( data ) {
if( data != \'\' ) {
ts_ets.tmp_parent.html( data );
}
}
});
});
$(document).ready(function() {
ts_ets.upload_frame = wp.media({
title: ets_strings.upload_title,
button: {
text: ets_strings.upload_add,
},
multiple: false
});
ts_ets.upload_frame.on( \'select\', function() {
ts_ets.selection = ts_ets.upload_frame.state().get(\'selection\');
ts_ets.selection.map( function( attachment ) {
if( attachment.id ) {
$.ajax({
url: ajaxurl,
method: \"POST\",
data: {
action: \'ts_ets_update\',
nonce: $(\'#ts_ets_nonce\').val(),
post_id: ts_ets.tmp_id,
thumb_id: attachment.id
},
success: function( data ) {
if( data != \'\' ) {
ts_ets.tmp_parent.html( data );
}
}
});
}
});
});
});
$(document).on( \'click\', \'button.ts-ets-add\', function(e) {
e.preventDefault();
ts_ets.tmp_id = $(this).data(\'id\');
ts_ets.tmp_parent = $(this).closest(\'td.ts-ets-option\');
if( ts_ets.upload_frame ) {
ts_ets.upload_frame.open();
}
});
})(jQuery);
方法二
这款是给大家一个更简单的版本,减少了上传与删除功能,只是一个显示调用功能,方便大小进行缩略图查看,因为更多的用户习惯是进入文章上传特色图片,很少人会通过后台列表就直接上传缩略图,所以今天给大家推荐一个更简单的方案。将下面的代码复制到当前 wordpress 主题的 functions.php 模板文件中,保存即可:
if ( !function_exists(\'fb_AddThumbColumn\') && function_exists(\'add_theme_support\') ) {
// for post and page
add_theme_support(\'post-thumbnails\', array( \'post\', \'page\' ) );
function fb_AddThumbColumn($cols) {
$cols[\'thumbnail\'] = __(\'Thumbnail\');
return $cols;
}
function fb_AddThumbValue($column_name, $post_id) {
$width = (int) 35;
$height = (int) 35;
if ( \'thumbnail\' == $column_name ) {
// thumbnail of WP 2.9
$thumbnail_id = get_post_meta( $post_id, \'_thumbnail_id\', true );
// image from gallery
$attachments = get_children( array(\'post_parent\' => $post_id, \'post_type\' => \'attachment\', \'post_mime_type\' => \'image\') );
if ($thumbnail_id)
$thumb = wp_get_attachment_image( $thumbnail_id, array($width, $height), true );
elseif ($attachments) {
foreach ( $attachments as $attachment_id => $attachment ) {
$thumb = wp_get_attachment_image( $attachment_id, array($width, $height), true );
}
}
if ( isset($thumb) && $thumb ) {
echo $thumb;
} else {
echo __(\'None\');
}
}
}
// for posts
add_filter( \'manage_posts_columns\', \'fb_AddThumbColumn\' );
add_action( \'manage_posts_custom_column\', \'fb_AddThumbValue\', 10, 2 );
// for pages
add_filter( \'manage_pages_columns\', \'fb_AddThumbColumn\' );
add_action( \'manage_pages_custom_column\', \'fb_AddThumbValue\', 10, 2 );
}
2020-01-31
猜你喜欢
-
手游【龙武】VM一键端+上线无限货币+图文教程+手工外网端+视频教程
2019-12-08 -
利物直播商城模块安装教程:解决安装出现空白或404
2020-07-31 -
WordPress如何实现给自定类型分类法投稿
2020-03-28 -
最新三国H5手游【乱世之君H5】天命神话VM一键即玩端+手工外网端+GM后台+视频教程
2020-04-17 -
WordPress免插件配置SMTP邮件功能教程
2020-03-28 -
WordPress如何实现部分文章内容需要登录后才能查看
2020-03-28 -
最新骆驼IPTV后端源码+前端APP+视频教程
2020-06-05 -
【精品教学】最新版同城进群付费完整详细高清录制搭建视频教程
2020-05-23 -
给WordPress博客添加返回顶部/底部的教程
2020-03-28 -
[带视频教程] 最新魔神争霸天机单机版 魔幻3D网游单机虚拟机镜像一键端 GM无限点券
2020-05-01
-
新彩虹自助服务订单5.5版彩虹代刷破解版免授权带视频教程
2019-08-15 -
WordPress获取文章所属分类名称、分类ID、分类别名
2020-03-28 -
wordpress登录界面样式优化
2020-02-11 -
wordpress禁止指定省份访问
2020-02-06 -
WordPress细节优化之给文章图片自动添加alt和title信息
2020-03-28 -
全新区块钱包奖励理财源码带安装视频教程
2020-04-03 -
2020聚合搜索V5.0泛目录站群PHP源码 带安装教程_源码下载
2020-03-08 -
【苍穹剑诀h5服务端】2020.06一键安装游戏客户端带授权后台[附外网搭建视频教程]
2020-06-27 -
WordPress如何调用全站、同分类随机文章
2020-03-28 -
WordPress正确引入加载JS、css文件的方法
2020-03-28
猜你在找
1、本站所有资源均来源于网络收集,一切版权©归原作者所有,请保留原版权信息。
2、本站分享仅供参考学习和演示,禁止商用,如需商用,请从正规渠道选择购买正版!使用正版!支持正版!维护一个良好的知识产权环境。
3、内容故是参考与学习,不确保能正常演示,也不包含其中的技术服务。
4、排除在某个功能上存在有其它的BUG或源码残缺的可能,购买后一率不能退款。
5、资源下载不含技术服务,需付费安装请联系客服100元/次。
6、文件储存在网盘,如发现链接或者密码有误,请联系客服。
7、如果付款后下载不了,请提交工单说明,客服会在24小时内解决,如果解决不了,会为您退款。
8、侵权反馈邮箱:yutongyuncom@qq.com
顺风猫 » WordPress如何为后台文章列表添加缩略图
常见问题FAQ
- 免费下载或者VIP会员专享资源能否直接商用?
- 本站所有资源版权均属于原作者所有,这里所提供资源均只能用于参考学习用,请勿直接商用。若由于商用引起版权纠纷,一切责任均由使用者承担。更多说明请参考 VIP介绍。
- 提示下载完但解压或打开不了?
- 找不到素材资源介绍文章里的示例图片?
- 顺风猫
- 2020-01-31Hi,初次和大家见面了,请多关照!