WP酷 WP酷
  • 主题
  • 教程
  • 插件
  • 官方 QQ 群
  • 建站推荐
  • 联系
  • nicetheme® 奈思主题
  • 注册
    登录
立即登录
  • 请到 [后台->外观->菜单] 中设置菜单
首页 › WordPress 教程 › WordPress调取文章第一张图片地址 支持外链

WordPress调取文章第一张图片地址 支持外链

PCDotFan9年前

若想实现外链特色图像的功能是可以使用插件:Featured Image Via URL,不过奇怪的是WordPress官方据说对它进行封杀,并且实际上只是将外链图片转存到本地服务器上而已。WP酷只能将以前积累的代码拿出来分享给大家,实现效果为:替换原本特色图像功能,设置外链缩略图。

 

Functions.php:

add_theme_support( 'post-thumbnails' );

function catch_that_image() {
global $post, $posts;
$first_img = '';
ob_start();
ob_end_clean();
$output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $matches);
$first_img = $matches [1] [0];
if(empty($first_img)){ //Defines a default image
$popimg=get_option( 'mao10_popimg');
$first_img = "$popimg";
}
return $first_img;
}

function mmimg($postID) {
	$cti = catch_that_image();
	$showimg = $cti;
	has_post_thumbnail();
	if ( has_post_thumbnail() ) { 
		$thumbnail_image_url = wp_get_attachment_image_src( get_post_thumbnail_id(), 'thumbnail');
		$shareimg = $thumbnail_image_url[0];
	} else { 
		$shareimg = $showimg;
	};
	return $shareimg;
} 

 

 

调用方式:

<img src="<?php echo mmimg(get_the_ID()); ?>" alt="" />

 

17
分享

本站下载管理系统「dl.mywpku.com」因年久失修而永久关闭。若需获得往年主题,请至 WP酷的百度网盘(提取码: 5rbm) 自行搜索下载。默认解压密码为「www.mywpku.com」。

对您造成的不便,敬请谅解!

PCDotFan 站长
文章 435评论 859
相关文章
  • [6.0 更新中文汉化]极度流行的SEO插件 – WordPress SEO by Yoast
  • WordPress 新用户注册邮件链接提示「您的密码重设链接无效」
  • AMP – 为 WordPress 移动站点操作全球访问优化
  • 去除谷歌服务 – WordPress 国外主题全方位加速
  • 使用 WP-CLI 简化 WordPress 操作流程
  • WordPress 基础开发 – 一探 WordPress Hook 机制及示例应用
  • 优化静态资源 – WordPress 国外主题全方位加速
  • WordPress 实现微信登录
评论 (22)
再想想
  • 冷劫

    在问一下 若是没有特色图片以及里面的图片要怎么办 这个能不能解决下!

    8年前
  • 冷劫

    哦哦 你看我的博客就知道了。。。顺带问一下 网站很丑吗?

    8年前
  • 英雄联盟之谁与争锋

    好文章,收藏了

    8年前
  • 网站大全

    好文章谢谢分享

    7年前
  • ‭

    默认图片怎么设置呢

    7年前
  • 走趣

    可以用,但是图片好像太大了

    7年前
  • alalala

    自动调取第一张图是实现了,能不能把文章中的外链图片自动剪裁到一个设定值在调用到首页。

    7年前
  • ddd

    这个调用方法能否说明一下?

    4年前
PCDotFan
站长
若无特别注明,则默认我发布的所有文章均为内容原创 / 翻译原创,转载时请保留来源。
435文章
859评论
3K获赞
猜你喜欢
funUIUI – 双栏灰白WordPress博客主题
9年前
Default featured image – 设置默认特色图像插件
10年前
使用 WP-CLI 简化 WordPress 操作流程
6年前
  • 杂记
  • WordPress 付费主题推荐
Copyright © 2023 WP酷. Designed by nicetheme. 琼ICP备13002067号-1