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

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

这里有一段很实用的代码,可以自动将文章中的第一张图片设置为特色图像,如果你手动设置了特色图像,可以覆盖这段代码,这样即使你忘记了设置,wp会自动调用文章中的第一张图片作为缩略图。

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

问题来了,如果我们的文章里没有图片,又忘了设置特色图像呢,那么我们可以让WP调用媒体库里的某张指定的图片作为缩略图。

    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,或者你阅读下面这篇文章来实现:
wordpress后台无插件显示文章和分类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

总结

1、你如果再制作自己的主题,在调试中,可以尝试一个你喜欢的方法。
2、你的站点已经发表了很多文章,并且都启用发特色图像,要慎用,可能你选择的某一个代码功能会影响你已经发布的文章不能正常显示!

3、特色图像会占用大量的服务器空间,因为每张图片都会裁剪成多张大小不同的缩略图方便在不同的位置调用,最主要的是不支持外链。


转载自511遇见网,原文链接:http://www.511yj.com/wordpress-featured-image.html

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在使用 WordPress 个客户开发项目的时候,经常会碰到需要设置多个日志缩略图的需求,当然我们可以通过自定义字段实现,但是这样使用起来不是非常方便,没有默认的设置特色图片那么好用,这时候,我一般使用 Multiple Post Thumbnails 这个 WordPress 件来解决。 Multiple Post Thumbnails 介绍 Multiple Post Thumbnails 是一个面向 WordPress 开发者的一个件,它定义了一套在后台增加多个缩略图的机制,以及在前端调用新增缩略图的方法,并且它定义的后台空间支持最新的 WordPress 3.5 图片上传方式,使用起来和默认的特色图片基本一致。 Multiple Post Thumbnails:可以定义多个日志缩略图的 WordPress 件 Multiple Post Thumbnails 使用方法 前面说过 Multiple Post Thumbnails 是一个面向开发者的件,所以需要你对主题进行一定的修改才能使用,下面简单介绍下使用方法: 1. 上传件并激活。  2. 如果你想给当前日志添加第二章缩略图,在当前主题的 functions.php 中添加如下代码: if (class_exists('MultiPostThumbnails')) {     new MultiPostThumbnails(         array(             'label' => 'Secondary Image',             'id' => 'secondary-image',             'post_type' => 'post'         )     ); } 当然你可以通过将 post_type 设置为 page,给静态页面设置第二章缩略图,也给自定义日志类型设置。    3. 最后通过下面代码显示定义的缩略图: <?php if (class_exists('MultiPostThumbnails')) : MultiPostThumbnails::the_post_thumbnail(get_post_type(), 'secondary-image'); endif; ?>
可以使用 WordPress 提供的 `wp_set_post_tags()` 函数来为文章添加标签。你可以在文章发布或更新时触发此函数,并将标签作为参数传递给它。 以下是一个简单的示例代码: ```php // 在文章发布或更新时触发 add_action('save_post', 'add_tags_to_post'); function add_tags_to_post($post_id) { // 检查是否为文章类型 if(get_post_type($post_id) != 'post') { return; } // 获取文章对象 $post = get_post($post_id); // 获取文章内容 $content = $post->post_content; // 将文章内容转换为标签数组 $tags = get_tags_from_content($content); // 为文章添加标签 if(!empty($tags)) { wp_set_post_tags($post_id, $tags, true); } } function get_tags_from_content($content) { // 从文章内容中提取标签 // 你可以编写自己的提取逻辑 // 这里只是一个示例 preg_match_all('/<tag>(.*?)<\/tag>/', $content, $matches); // 返回标签数组 return $matches[1]; } ``` 在上面的代码中,我们定义了一个 `add_tags_to_post()` 函数,它会在文章发布或更新时被触发。我们首先检查文章类型,然后获取文章对象和内容。接着,我们调用了 `get_tags_from_content()` 函数,该函数将文章内容转换为标签数组。最后,我们调用了 `wp_set_post_tags()` 函数,将标签数组添加文章中。 需要注意的是,`get_tags_from_content()` 函数中的正则表达式只是一个示例。你需要根据自己的需求编写自己的提取逻辑。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值