WordPress教程

WordPress不用插件实现多张特色图片缩略图

阿里云

WordPress 的文章、页面或者自定义文章类型开启特色图片,通过注册这个类型时,在 support 中添加参数“thumbnail”实现,比如:

  1. register_post_type('My CPT', array(
  2. 	'label' => 'my_cpt',
  3. 	'description' => '',
  4. 	'public' => true,
  5. 	'show_ui' => true,
  6. 	'show_in_menu' => true,
  7. 	'capability_type' => 'post',
  8. 	'hierarchical' => false,
  9. 	'rewrite' => array('slug' => 'product'),
  10. 	'query_var' => true,
  11. 	'supports' => array('title','editor','thumbnail') //这里有了thumbnail就能为my_cpt这个类型添加缩略图
  12. 	)
  13. );
也想出现在这里?联系我们
创客主机

这个缩略图有没有可能变成多张呢?比如这样:

查了老半天,WordPress 本身并没有提供多张特色图片的 API,但这个功能完全可以通过自定义字段实现。本文即是这个原理,不多废话,上代码:

  1. <?php
  2. add_action( 'after_setup_theme', 'brain1981_featured_img_setup' );
  3. function brain1981_featured_img_setup(){
  4. 	add_action( 'add_meta_boxes', 'brain1981_feature_img_meta_box' );
  5. 	add_action( 'save_post', 'brain1981_feature_img_meta_box_save' );
  6. }
  7.  
  8. function brain1981_feature_img_meta_box(){
  9. 	//$post_types = array('post','page');//给原生的文章和页面开启额外缩略图
  10. 	$post_types = array('my_cpt');
  11. 	foreach($post_types as $post_type){
  12. 		add_meta_box('brain1981_feature_img_meta_box', __( 'More Product Images'), 'brain1981_feature_img_meta_box_cont', $post_type, 'side','low');
  13. 	}
  14. }
  15.  
  16. function brain1981_feature_img_meta_box_cont($post){
  17. 	//以下这个数组可以任意扩展,比如featured_image_4,5,6...
  18. 	$meta_keys = array('featured_image_2','featured_image_3');
  19. 	foreach($meta_keys as $meta_key){
  20. 		$image_meta_val=get_post_meta( $post->ID, $meta_key, true);
  21. 		?>
  22. 		<div class="cpm_image_wrapper" id="<?php echo $meta_key; ?>_wrapper" style="margin-bottom:28px;" >
  23. 			<a href="###" class="cpm_addimage cpm_addimage_img" data-metakay="<?php echo $meta_key; ?>" style="display: <?php echo ($image_meta_val!=''?'block':'none'); ?>">
  24. 				<img src="<?php echo ($image_meta_val!=''?wp_get_attachment_image_src( $image_meta_val, "full")[0]:''); ?>" style="width:100%;" alt="">
  25. 			</a>
  26. 			<a href="###" class="cpm_removeimage" style="display: <?php echo ($image_meta_val!=''?'block':'none'); ?>" data-metakay="<?php echo $meta_key; ?>"><?php _e('Remove featured image'); ?></a>
  27. 			<a href="###" class="cpm_addimage" style="display: <?php echo ($image_meta_val!=''?'none':'block'); ?>" data-metakay="<?php echo $meta_key; ?>"><?php _e('Set featured image'); ?></a>
  28. 			<input type="hidden" name="<?php echo $meta_key; ?>" id="<?php echo $meta_key; ?>" value="<?php echo $image_meta_val; ?>" />
  29. 		</div>
  30. 	<?php } ?>
  31. <script>
  32. jQuery(function(){
  33. 	jQuery(".cpm_addimage").on("click",function(){
  34. 		var key = jQuery(this).data("metakay");
  35. 		cpm_meta_box_add_img(key);
  36. 	});
  37. 	function cpm_meta_box_add_img(key){
  38. 		var $wrapper = jQuery('#'+key+'_wrapper');
  39.  
  40. 		cpm_meta_box_img_uploader = wp.media.frames.file_frame = wp.media({
  41. 			title: '<?php _e('select image','kais'); ?>',
  42. 			button: {
  43. 				text: '<?php _e('select image','kais'); ?>'
  44. 			},
  45. 			multiple: false
  46. 		});
  47. 		cpm_meta_box_img_uploader.on('select', function() {
  48. 			var attachment = cpm_meta_box_img_uploader.state().get('selection').first().toJSON();
  49. 			var img_url = attachment['url'];
  50. 			var img_id = attachment['id'];
  51. 			$wrapper.find('input#'+key).val(img_id);
  52. 			$wrapper.find('img').attr('src',img_url);
  53. 			$wrapper.find('a.cpm_removeimage').show();
  54. 			$wrapper.find('a.cpm_addimage').hide();
  55. 			$wrapper.find('.cpm_addimage_img').show();
  56.  
  57. 		});
  58. 		cpm_meta_box_img_uploader.on('open', function(){
  59. 			var selection = cpm_meta_box_img_uploader.state().get('selection');
  60. 			var selected = $wrapper.find('input#'+key).val();
  61. 			if(selected){
  62. 			    selection.add(wp.media.attachment(selected));
  63. 			}
  64. 		});
  65. 		cpm_meta_box_img_uploader.open();
  66. 		return false;
  67. 	}
  68.  
  69. 	jQuery(".cpm_removeimage").on("click",function(){
  70. 		var key = jQuery(this).data("metakay");
  71. 		cpm_meta_box_remove_img(key);
  72. 	});
  73. 	function cpm_meta_box_remove_img(key){
  74. 		var $wrapper = jQuery('#'+key+'_wrapper');
  75. 		$wrapper.find('input#'+key).val('');
  76. 		$wrapper.find('a.cpm_removeimage').hide();
  77. 		$wrapper.find('a.cpm_addimage').show();
  78. 		$wrapper.find('.cpm_addimage_img').hide();
  79. 		return false;
  80. 	}
  81. });	
  82. </script>
  83. 	<?php
  84. 	wp_nonce_field( 'brain1981_feature_img_meta_box', 'brain1981_feature_img_meta_box_nonce' );
  85. }
  86.  
  87. function brain1981_feature_img_meta_box_save($post_id){
  88. 	if ( ! current_user_can( 'edit_posts', $post_id ) ){ return 'not permitted'; }
  89. 	if (isset( $_POST['brain1981_feature_img_meta_box_nonce'] ) && wp_verify_nonce($_POST['brain1981_feature_img_meta_box_nonce'],'brain1981_feature_img_meta_box' )){
  90. 		//以下这个数组可以任意扩展,比如featured_image_4,5,6...,但必须和brain1981_feature_img_meta_box_cont函数中一致
  91. 		$meta_keys = array('featured_image_2','featured_image_3');
  92. 		foreach($meta_keys as $meta_key){
  93. 		    if(isset($_POST[$meta_key]) && intval($_POST[$meta_key])!=''){
  94. 		    	update_post_meta( $post_id, $meta_key, intval($_POST[$meta_key]));
  95. 		    }else{
  96. 		    	update_post_meta( $post_id, $meta_key, '');
  97. 		    }
  98. 		}
  99. 	}
  100. }

以下 CSS 非必须,是为了让后台这张图片在透明的时候能显示网格,和 WP 官方 UI 一致:

  1. .cpm_image_wrapper img{
  2. 	background-image:linear-gradient(45deg,#c3c4c7 25%,transparent 25%,transparent 75%,#c3c4c7 75%,#c3c4c7),linear-gradient(45deg,#c3c4c7 25%,transparent 25%,transparent 75%,#c3c4c7 75%,#c3c4c7);
  3. 	background-position: 0 0,10px 10px;
  4. 	background-size: 20px 20px;
  5. }
  6. ?>

在页面调用这些新增的特色图片:

  1. $image_id = get_post_meta( get_the_ID(), 'featured_image_2', true);
  2. echo wp_get_attachment_image($image_id,'full');

以上代码即是通过给 featured_image_2、featured_image_3 这些自定义字段存储上传图片的 id,实现多张自定义特色图片。WP 本身的自定义图片其实也是这个原理,只是变量名称不同,并且有几个内部的方法可以调用;而调用这些自定义的特色图片则只能通过通用的方法。其实我更喜欢用这些通用方法调用任何附件图片,比唯一那张特色图片独有的方法要普适好记。

WordPress 不用插件实现多张特色图片缩略图

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

收藏
(0)

发表回复

热销模板

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

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