在wordpress中想实现发布文章贴图片网址就直接显示图片,参照网上的代码写了一段这样的,没效果。
function embed_images($content){$embed_images = preg_replace_callback('/((^http[s]{0,1}:)\/\/(.+).(gif|jpg|bmp|png))/i', "addimg",$content);return $embed_images;}
function addimg($url){global $post;$title = $post ->post_title;$imgurl = preg_replace('/((^http[s]{0,1}:)\/\/(.+).(gif|jpg|bmp|png))/i', '<img src="$1" alt="'.$title.'" />',$url[0]);if($imgurl !== $url[0]){return $imgurl;}}}add_filter(the_content', 'embed_images');
但是改成下面的就可以显示图片了(相比上面的只替换了第一段的正则),不过这个同时把正文的网址链接也直接去掉了。怎样能显示图片,但是不要删除链接呢?求解。。。
function embed_images($content){$embed_images = preg_replace_callback('/https?:\/\/([A-z0-9]+[_\-]?[A-z0-9]+\.)*[A-z0-9]+\-?[A-z0-9]+\.[A-z]{2,}(\/.*)*\/?/i', "addimg",$content);return $embed_images;}
function addimg($url){global $post;$title = $post ->post_title;$imgurl = preg_replace('/((^http[s]{0,1}:)\/\/(.+).(gif|jpg|bmp|png))/i', '<img src="$1" alt="'.$title.'" />',$url[0]);if($imgurl !== $url[0]){return $imgurl;}}}add_filter(the_content', 'embed_images');
function embed_images($content){$embed_images = preg_replace_callback('/((^http[s]{0,1}:)\/\/(.+).(gif|jpg|bmp|png))/i', "addimg",$content);return $embed_images;}
function addimg($url){global $post;$title = $post ->post_title;$imgurl = preg_replace('/((^http[s]{0,1}:)\/\/(.+).(gif|jpg|bmp|png))/i', '<img src="$1" alt="'.$title.'" />',$url[0]);if($imgurl !== $url[0]){return $imgurl;}}}add_filter(the_content', 'embed_images');
但是改成下面的就可以显示图片了(相比上面的只替换了第一段的正则),不过这个同时把正文的网址链接也直接去掉了。怎样能显示图片,但是不要删除链接呢?求解。。。
function embed_images($content){$embed_images = preg_replace_callback('/https?:\/\/([A-z0-9]+[_\-]?[A-z0-9]+\.)*[A-z0-9]+\-?[A-z0-9]+\.[A-z]{2,}(\/.*)*\/?/i', "addimg",$content);return $embed_images;}
function addimg($url){global $post;$title = $post ->post_title;$imgurl = preg_replace('/((^http[s]{0,1}:)\/\/(.+).(gif|jpg|bmp|png))/i', '<img src="$1" alt="'.$title.'" />',$url[0]);if($imgurl !== $url[0]){return $imgurl;}}}add_filter(the_content', 'embed_images');