- 资源介绍
- 更新记录
- 安装教程
目前很多 <span 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 );
}
文章来自大资源网https://www.dzy10.com转载请注明出处,并保留本链接,谢谢!
猜你喜欢
-
WordPress主题美化_Ripro主题网站图片放大效果
2020-06-08 -
WordPress获取指定/当前分类目录的文章数量
2020-06-08 -
WordPress文章上一篇下一篇显示缩略图
2020-06-08 -
wordpress修改wp_admin隐藏后台管理登录地址确保WordPress网站后台安全
2020-06-08 -
WordPress优化之去掉分类链接中的category
2020-06-08 -
WordPress如何给关键词和描述的结尾加上斜杠/
2020-07-05 -
ripro子主题jizhi_chlid极致主题如何设置修改顶部网站相关?
2020-07-02 -
20年来WordPress最新安装的详细教程还在问wordpress如何安装吗?这篇文章解决了这个问题
2020-06-03 -
WordPress自动截取部分文章内容做为文章摘要
2020-06-08 -
WordPress获取本周/今日/24小时内更新的文章数量
2020-06-08
-
WordPress后台如何禁止编辑主题和插件
2020-06-08 -
WordPress如何在删除文章时自动删除文章内的图片附件
2020-06-08 -
WordPress文章随机显示缩略图的实现方法
2020-06-08 -
用代码完美实现wordpress蜘蛛爬行记录生成,远离插件,热爱速度!
2020-06-08 -
ripro子主题jizhi_chlid极致主题如何修改顶部最新公告?
2020-07-02 -
wordpress登录界面样式优化
2020-06-08 -
WordPress根据分类别名或ID选择内容页模板
2020-06-08 -
WordPress如何调用指定ID文章
2020-06-08 -
WordPress博客如何获取站点总访问量
2020-06-08 -
WordPress如何在后台查看分类ID
2020-06-08
猜你在找
常见问题FAQ
- 免费下载或者VIP会员专享资源能否直接商用?
- 本站所有资源版权均属于原作者所有,这里所提供资源均只能用于参考学习用,请勿直接商用。若由于商用引起版权纠纷,一切责任均由使用者承担。更多说明请参考 VIP介绍。
- 提示下载完但解压或打开不了?
- 找不到素材资源介绍文章里的示例图片?
- 大资源
- 2020-06-08Hi,初次和大家见面了,请多关照!
最后编辑:2020-10-20


