当前位置: 首页 » 网站建设 » wordpress教程 » 正文

[后台增强]在wordpress文章列表集成特色图片功能

发布时间:2025-01-16 以下文章来源于网友投稿,内容仅供参考!

这个功能很简单就是在您的wordpress后台文章列表里面的右侧可以显示出当时有设置特色图片文章的图片缩略图,很实力的一个小的增强功能,可以更方法的将文章封面展示在列表里,免除了打开内容或是跳转前端确认文章封面特色图片的步骤,而且操作方法也是简单易学,下面测速网把代码贴出来分享给大家,喜欢wordpress的小伙伴可以动手折腾一下。
首页还是找到我们主题的functions.php文件在里面添加以下代码

area data-settings="dblclick" readonly >class doocii_Easy_Thumbnail_Switcher {public $add_new_str;public $change_str;public $remove_str;public $upload_title;public $upload_add;public $/confirm/i_str;public function __construct() {$this->add_new_str = __( '添加');$this->change_str = __( '更改');$this->remove_str = __( '移除');$this->upload_title = __( '上传特色图片');$this->upload_add = __( '确定');$this->/confirm/i_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/i' => $this->/confirm/i_str,) );}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" data-id="%s">%s</button>', esc_attr( $id ), $this->change_str );echo sprintf( ' <button type="button" data-id="%s">%s</button>', esc_attr( $id ), $this->remove_str );} else {echo sprintf( '<button type="button" data-id="%s">%s</button>', esc_attr( $id ), $this->add_new_str );}break;}}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" data-id="%s">%s</button>', esc_attr( $id ), $this->change_str );echo sprintf( ' <button type="button" data-id="%s">%s</button>', esc_attr( $id ), $this->remove_str );wp_die();}public function remove() {// Check if all required data are set or notif( !isset( $_POST['nonce'] ) || !isset( $_POST['post_id'] ) ) {wp_die();}// Verify nonceif( !wp_verify_nonce( $_POST['nonce'], 'ts_ets_nonce' ) ) {wp_die();}$id = $_POST['post_id'];delete_post_thumbnail( $id );echo sprintf( '<button type="button" data-id="%s">%s</button>', esc_attr( $id ), $this->add_new_str );wp_die();}}new doocii_Easy_Thumbnail_Switcher();area>161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157class doocii_Easy_Thumbnail_Switcher {public $add_new_str;public $change_str;public $remove_str;public $upload_title;public $upload_add;public $/confirm/i_str;public function __construct() {$this->add_new_str = __( '添加');$this->change_str = __( '更改');$this->remove_str = __( '移除');$this->upload_title = __( '上传特色图片');$this->upload_add = __( '确定');$this->/confirm/i_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/i' => $this->/confirm/i_str,) );}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" data-id="%s">%s</button>', esc_attr( $id ), $this->change_str );echo sprintf( ' <button type="button" data-id="%s">%s</button>', esc_attr( $id ), $this->remove_str );} else {echo sprintf( '<button type="button" data-id="%s">%s</button>', esc_attr( $id ), $this->add_new_str );}break;}}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" data-id="%s">%s</button>', esc_attr( $id ), $this->change_str );echo sprintf( ' <button type="button" data-id="%s">%s</button>', esc_attr( $id ), $this->remove_str );wp_die();}public function remove() {// Check if all required data are set or notif( !isset( $_POST['nonce'] ) || !isset( $_POST['post_id'] ) ) {wp_die();}// Verify nonceif( !wp_verify_nonce( $_POST['nonce'], 'ts_ets_nonce' ) ) {wp_die();}$id = $_POST['post_id'];delete_post_thumbnail( $id );echo sprintf( '<button type="button" data-id="%s">%s</button>', esc_attr( $id ), $this->add_new_str );wp_die();}}new doocii_Easy_Thumbnail_Switcher();

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

area data-settings="dblclick" readonly >(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);area>161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687(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);

  • • 开启wds是什么意思?无线路由器wds是怎么开启?

    开启wds是什么意思?WDS是英文Wireless Distribution System的简称,中文名称是:无线分布式系统。WDS功能就是一种帮助无线基站与

  • • 系统boot是什么意思?电脑出现boot manager怎么办?

    系统boot是什么意思1、开机后我们连续按启动热键,进入bios界面,我们就可以看到boot了。2、boot有启动的意思,在电脑中就是指电

  • • win10系统msconfig无法正常启动怎么办?win10系统msconfig怎么设置最

    msconfig打不开解决办法:1.先打开电脑开始菜单,在操作窗口输入regedit,打开注册表编辑器。然后将HKEY_LOCAL_MACHINE\SOFTWARE

  • • 金士顿u盘怎么写保护?金士顿U盘恢复出厂设置怎么操作?

    金士顿u盘怎么写保护?方法一:如果你的U盘如我简介中说的那样,有一个自带的物理写保护的小 开关 ,就观察一下是不是已经将开关

  • • 苹果AirPods Pro 2发布 搭载H2芯片降噪能力翻倍

    9月8日凌晨,苹果正式发布第二代主动降噪耳机AirPods Pro 2,搭载H2芯片,降噪能力翻倍,而且续航也飚至30小时。得益于全新的H2

  • biubiu加速器
    九游优盟游戏推广平台
    九游推广联盟
    digdig游戏入口
    4999小游戏大全
    九游游戏网
    站长素材
    植物大战僵尸杂交免费版
    站长之家
    测速网在线测网速
    354211邮编查询,354211邮政编码查询
    354209邮编查询,354209邮政编码查询
    353132邮编查询,353132邮政编码查询
    354215邮编查询,354215邮政编码查询
    353121邮编查询,353121邮政编码查询
    354213邮编查询,354213邮政编码查询
    353125邮编查询,353125邮政编码查询
    354201邮编查询,354201邮政编码查询
    353101邮编查询,353101邮政编码查询
    354200邮编查询,354200邮政编码查询