WordPress免插件自动为文章添加设置特色图像的三种方法

作者: admin 分类: 技术 发布时间: 2023-10-07 20:09

wordpress特色图像可以为每篇文章设置一个缩略图,但必须是手动设置,下面我们通过代码实现自动把文章中的第一张图片添加为缩略图,如果文章中没有图片,我们可以调用媒体库中的某个图片作为文章的缩略图,或者我们设定一个文件夹里面上传我们需要设定为缩略图的图片。

在当前主题的functions.php里添加以下代码

方法一:

    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);
                }
            }
        }
    }  //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');

方法二:自动调用媒体库中的图片作为缩略图

    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, '66');
            }
        }
    }  //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, ‘66‘是媒体库中某张图片的id
如何查看媒体库里某张图片的ID呢?
WP-后台-多媒体-媒体库,采用一个列表浏览方式,把鼠标指向图片,在浏览器的下面会显示图片的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了,所以注释掉了一个。

PS:调用特色图像和设置大小

要让你的主题支持特色图像必须在functions.php中加入以下代码:

    if ( function_exists('add_theme_support') )add_theme_support('post-thumbnails');

在post模板中调用:

    <?php
    if ( has_post_thumbnail() ) {// check if the post has a Post Thumbnail assigned to it.
      the_post_thumbnail();
    }
    ?>
    <?php the_content(); ?>

可以调用不同尺寸的图片:

    the_post_thumbnail();           // 无参数,默认调用Thumbnail
    the_post_thumbnail('thumbnail');// Thumbnail(默认尺寸 150px150px max)
    the_post_thumbnail('medium');   // Medium resolution(default300px300px max)
    the_post_thumbnail('large');    // Large resolution(default640px640px max)
    the_post_thumbnail('full');     // Full resolution(original size uploaded)
    the_post_thumbnail( array(100,100) );//Other resolutions

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注

标签云
站点地图 Sitemap
申请 友情链接
网站运行