WordPress如何在文章列表编辑特色图像

在文章列表编辑特色图像,看到这句话大概就知道今天要分享的是什么功能了。

这是一个非常实用的功能,它能使我们在WordPress后台编辑文章时方便很多。

如果你的主题支持缩略图请首先在functions.php中添加

//开启文章缩略图

add_theme_support( \'post-thumbnails\' );

首先我们在后台文章列表添加数组, 复制下面的代码到你的functions.php中。

/*
 * 添加数组到文章列表
 */
add_filter(\'manage_post_posts_columns\', \'lb_featured_image_column\');
function lb_featured_image_column( $column_array ) {
	$column_array = array_slice( $column_array, 0, 1, true )
	+ array(\'featured_image\' => \'特色图像\')
	+ array_slice( $column_array, 1, NULL, true );
 
	return $column_array;
}
 
/*
 * 使用钩子完善数组
 */
add_action(\'manage_posts_custom_column\', \'lb_render_the_column\', 10, 2);
function lb_render_the_column( $column_name, $post_id ) {
	if( $column_name == \'featured_image\' ) {
		if( has_post_thumbnail( $post_id ) ) {
			$thumb_id = get_post_thumbnail_id( $post_id );
			echo \'<img data-id=\"\' . $thumb_id . \'\" src=\"\' . wp_get_attachment_url( $thumb_id ) . \'\" />\';
		} else {
			echo \'<img data-id=\"-1\" src=\"\' . get_stylesheet_directory_uri() . \'/placeholder.png\" />\';
		}
	}
}

然后,我们添加一些CSS样式来美化数组。

add_action( \'admin_head\', \'lb_custom_css\' );
function lb_custom_css(){
 
	echo \'<style>
		#featured_image{
			width:120px;
		}
		td.featured_image.column-featured_image img{
			max-width: 100%;
			height: auto;
		}
 
		/* some styles to make Quick Edit meny beautiful */
		#lb_featured_image .title{margin-top:10px;display:block;}
		#lb_featured_image a.lb_upload_featured_image{
			display:inline-block;
			margin:10px 0 0;
		}
		#lb_featured_image img{
			display:block;
			max-width:200px !important;
			height:auto;
		}
		#lb_featured_image .lb_remove_featured_image{
			display:none;
		}
	</style>\';
 
}

完成以上步骤后,在后台文章就可以查看效果了。

接下来添加JS代码使特色图像可以快速编辑和更新,首先排队加载WordPress默认上传

add_action( \'admin_enqueue_scripts\', \'lb_include_myuploadscript\' );
function lb_include_myuploadscript() {
	if ( ! did_action( \'wp_enqueue_media\' ) ) {
		wp_enqueue_media();
	}
}

然后在后台文章页面添加快速编辑字段:

add_action(\'quick_edit_custom_box\',  \'lb_add_featured_image_quick_edit\', 10, 2);
function lb_add_featured_image_quick_edit( $column_name, $post_type ) {
 
	if ($column_name != \'featured_image\') return;
	echo \'<fieldset id=\"lb_featured_image\" class=\"inline-edit-col-left\">
		<div class=\"inline-edit-col\">
			<span class=\"title\">特色图像</span>
			<div>
				<a href=\"#\" rel=\"external nofollow\"  rel=\"external nofollow\"  class=\"lb_upload_featured_image\">设置特色图像</a>
				<input type=\"hidden\" name=\"_thumbnail_id\" value=\"\" />
			</div>
			<a href=\"#\" rel=\"external nofollow\"  rel=\"external nofollow\"  class=\"lb_remove_featured_image\">移除特色图像</a>
		</div></fieldset>\';
 
}

最后就是更新保存了,添加下面代码即可。

add_action(\'admin_footer\', \'lb_quick_edit_js_update\');
function lb_quick_edit_js_update() {
 
	global $current_screen;
	if (($current_screen->id != \'edit-post\') || ($current_screen->post_type != \'post\'))
		return;
 
		?><script>
		jQuery(function($){
 
			$(\'body\').on(\'click\', \'.lb_upload_featured_image\', function(e){
				e.preventDefault();
				var button = $(this),
				 custom_uploader = wp.media({
					title: \'设置特色图像\',
					library : { type : \'image\' },
					button: { text: \'设置特色图像\' },
				}).on(\'select\', function() {
					var attachment = custom_uploader.state().get(\'selection\').first().toJSON();
					$(button).html(\'<img src=\"\' + attachment.url + \'\" />\').next().val(attachment.id).parent().next().show();
				}).open();
			});
 
			$(\'body\').on(\'click\', \'.lb_remove_featured_image\', function(){
				$(this).hide().prev().val(\'-1\').prev().html(\'设置特色图像\');
				return false;
			});
 
			var $wp_inline_edit = inlineEditPost.edit;
			inlineEditPost.edit = function( id ) {
				$wp_inline_edit.apply( this, arguments );
 				var $post_id = 0;
				if ( typeof( id ) == \'object\' ) { 
					$post_id = parseInt( this.getId( id ) );
				}
 
				if ( $post_id > 0 ) {
					var $edit_row = $( \'#edit-\' + $post_id ),
							$post_row = $( \'#post-\' + $post_id ),
							$featured_image = $( \'.column-featured_image\', $post_row ).html(),
							$featured_image_id = $( \'.column-featured_image\', $post_row ).find(\'img\').attr(\'data-id\');
 
 
					if( $featured_image_id != -1 ) {
 
						$( \':input[name=\"_thumbnail_id\"]\', $edit_row ).val( $featured_image_id ); // ID
						$( \'.lb_upload_featured_image\', $edit_row ).html( $featured_image ); // 图像 HTML
						$( \'.lb_remove_featured_image\', $edit_row ).show(); // 移除链接
 
					}
				}
 		}
	});
	</script>
<?php
}

这篇WordPress如何在文章列表编辑特色图像的教程到此就结束了

提高网络安全意识,养成良好上网习惯,网络下载文件先扫描杀毒。不要随意点击不明邮件中的链接、图片、文件,适当设置找回密码的提示问题。当收到与个人信息和金钱相关(如中奖、集资等)的邮件时要提高警惕。不要轻易打开陌生人发送至手机的链接和文件。在微信、QQ等程序中关闭定位功能,仅在需要时开启蓝牙。安装手机安全防护软件,经常对手机系统进行扫描。不随意连接不明Wi-Fi、刷不明二维码。保证手机随身携带,建议手机支付客户端与手机绑定,开启实名认证。从官方网站下载手机支付客户端和网上商城 应用。使用手机支付服务前,按要求在手机上安装专门用于安全防范的插件。登录手机支付应用、网上商城时,勿选择“记住密码”选项。通过网络购买商品时,仔细查看登录的网站域名是否正确,谨慎点击商家从即时通讯工具上发送的支付链接。谨慎对待手机上收到的中奖、积分兑换等信息,切勿轻易点击短信中附带的不明网址。资源收集自互联网,仅供用于学习和测试,请勿用于商业。资源如有侵权,请联系站长删除,敬请谅解!

原文
---本内容完成,💞请分享---
WWW.A5.ORG.CN
👉喜欢💞点赞👍分享🙏
点赞8 分享
相关推荐内容
热门文章
【纯源码】梦幻飞蛾源码 四界面切换 元神系统 天梯系统 18门派都可以拜师-A5资源网
安卓韩剧TV v5.6.2绿化版-A5资源网
最新浪子云支付开源版源码-A5资源网
安卓南瓜影视去广告版v1.3.6.5 秒加载全部免费-A5资源网
最新无广告二次解析接口_独家带后台:手机端,PC端-全网VIP视频解析源码-A5资源网
安卓相片大师 PhotoDirector v15.1.2高级版-A5资源网
通用按键精灵(自动按键)下载绿色版-A5资源网
获取通讯录源码-A5资源网
最新域名防红系统全解密教程_附源码-A5资源网
最新微信运动一键修改网页源码(亲测可用)-A5资源网
安卓加菲猫影视v1.5.4绿化版-A5资源网
首席赚钱省钱专家小程序1.5.8+前端+修复商品和搜索关键词显示异常的问题-A5资源网
目前功能最多的带流量主微信工具箱小程序-A5资源网
PC小米运动定时刷步数v3.2-A5资源网
【抖音客】新版UI短视频点赞任务系统完美运营级别[等级功能+信誉积分+保证金]-A5资源网
最新打卡运动达人区块链运动商城源码-A5资源网
最新彩虹daishua5.5破 解除后版本-A5资源网
安卓啾啾啾v2.0 百度云网盘资源搜索工具-A5资源网
安卓位置修改器v1.3.2官方版-A5资源网
朋友圈广告助手V10.6.1源码下载分享-A5资源网