本文整理自网络,侵删。
这个动作我们使用一个hook来实现:
代码如下:
add_action('publish_post', 'fetch_images',999);
add_action('publish_post', 'fetch_images',999);
再创建一个fetch_images函数,来实现本文所说的所有功能。
代码如下:
function fetch_images( $post_ID ){
if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE ) return;
if ( !current_user_can('edit_post', $post_ID) ) return;
$post = get_post($post_ID);
function fetch_images( $post_ID ){
if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE ) return;
if ( !current_user_can('edit_post', $post_ID) ) return;
$post = get_post($post_ID);
接下来,我们要获取文章内容中的第一张图片:
代码如下:
$first_image = '';
preg_match('/<img.+src=[\'\"]([^\'\"]+)[\'\"].* \/>/i',$post->post_content,$images);
if(!empty($images))foreach($images as $image){
if(strpos($image,'http') === 0){
$first_image = $images[1];
break;
}
}
$first_image = '';
preg_match('/<img.+src=[\'\"]([^\'\"]+)[\'\"].* \/>/i',$post->post_content,$images);
if(!empty($images))foreach($images as $image){
if(strpos($image,'http') === 0){
$first_image = $images[1];
break;
}
}
相关阅读 >>
wordpress添加并整合google自定义搜索实现步骤及代码
bluehost服务器主机购买及安装wordpress的教程
解析wordpress中控制用户登陆和判断用户登陆的php函数
docker搭建 nginx+php+mysql 环境并部署wordpress实践
更多相关阅读请进入《wordpress》频道 >>
相关推荐
评论
管理员已关闭评论功能...
- 欢迎访问木庄网络博客
- 可复制:代码框内的文字。
- 方法:Ctrl+C。