0°

wordpress自动设置特色图片

3082 个字符,0 张图片,大约需要 4 分钟阅读

自动设置第一张图片设置为特色图像

丢进function.php

function autoset_featured() {
          global $post;
          $already_has_thumb = has_post_thumbnail($post->ID);
              if (!$already_has_thumb)  {
              $attached_image = get_children( "post_parent=$post->ID&post_type=attachment&post_mime_type=image&numberposts=1" );
                          if ($attached_image) {
                                foreach ($attached_image as $attachment_id => $attachment) {
                                set_post_thumbnail($post->ID, $attachment_id);
                                }
                           }
                        }
      }  //end function
add_action('the_post', 'autoset_featured');
add_action('save_post', 'autoset_featured');
add_action('draft_to_publish', 'autoset_featured');
add_action('new_to_publish', 'autoset_featured');
add_action('pending_to_publish', 'autoset_featured');
add_action('future_to_publish', 'autoset_featured');

自动调用媒体库中的图片作为缩略图

function wpforce_featured() {
    global $post;
    $already_has_thumb = has_post_thumbnail($post->ID);
    if (!$already_has_thumb)  {
       $attached_image = get_children( "post_parent=$post->ID&post_type=attachment&post_mime_type=image&numberposts=1" );
        if ($attached_image) {
           foreach ($attached_image as $attachment_id => $attachment) {
              set_post_thumbnail($post->ID, $attachment_id);
            }
        } else {
            set_post_thumbnail($post->ID, '123');
        }
    }
}  //end function
add_action('the_post', 'wpforce_featured');
add_action('save_post', 'wpforce_featured');
add_action('draft_to_publish', 'wpforce_featured');
add_action('new_to_publish', 'wpforce_featured');
add_action('pending_to_publish', 'wpforce_featured');
add_action('future_to_publish', 'wpforce_featured');

其中 $post->ID, ‘123’是媒体库中某张图片的id

自定义图片作为特色图像

/**
 * 添加特色缩略图支持相关文章带有缩略图
 */
if ( function_exists('add_theme_support') )add_theme_support('post-thumbnails');
//让你的主题开启特色图像功能
function post_thumbnail_src(){
  global $post;
   if( $values = get_post_custom_values("thumb") ) {//输出自定义域图片地址
     $values = get_post_custom_values("thumb");
     $post_thumbnail_src = $values [0];
  } elseif( has_post_thumbnail() ){//如果有特色缩略图,则输出缩略图地址
     $thumbnail_src = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID),'thumbnail');
//其中full可以修改的,可以为thumbnail, medium, large or full(分别代表最小的缩略图、中等、大和原始尺寸)
    $post_thumbnail_src = $thumbnail_src [0];
 } else {
	$post_thumbnail_src = '';
	ob_start();
	ob_end_clean();
	$output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $matches);
	$post_thumbnail_src = $matches [1] [0];   //获取该图片 src
	if(empty($post_thumbnail_src)){	//如果日志中没有图片,则显示随机图片
		$random = mt_rand(1, 10);
		echo get_bloginfo('template_url');
		echo '/images/pic/'.$random.'.jpg';
		//如果日志中没有图片,则显示默认图片
		//echo '/images/default_thumb.jpg';
	}
};
echo $post_thumbnail_src;
}

在主题下建立图像文件夹/images/pic/,里面上传你先显示的图片,作为随机调用,而/images/default_thumb.jpg最为默认调用,当然这两个功能我们选择一个就OK了,所以注释掉了一个。

除非注明,否则均为喵喵喵博客原创文章,转载请以链接形式标明本文地址

本文链接:https://www.miaomiaomiao.org/544.html

0 条回复 A 作者 M 管理员
    当上帝赐给你荒野时,就意味着,他要你成为高飞的鹰
欢迎您,新朋友,感谢参与互动!欢迎您 {{author}},您在本站有{{commentsCount}}条评论