本文整理自网络,侵删。
这个动作我们使用一个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中调用评论模板和循环输出评论的php函数
wordpress中强大的调用文章函数query posts 用法
wordpress 文章分页 实用的wordpress长文章分页代码
更多相关阅读请进入《wordpress》频道 >>
相关推荐
评论
管理员已关闭评论功能...
- 欢迎访问木庄网络博客
- 可复制:代码框内的文字。
- 方法:Ctrl+C。