wordpress 获取文章内所有图片个数与图片地址例子

WordPress获取文章中的图片个数

复制以下代码到当前使用主题的functions.php文件中,然后在文章列表主循环或文章页中调用该函数即可。

注:xiu主题已有该功能。

WordPress获取文章中的图片个数函数

/*
 * 获取文章中的图片个数 (使用在文章列表主循环中、或文章页中)

 */
if( !function_exists('get_post_images_number') ){
 function get_post_images_number(){
     global $post;
     $content = $post->post_content; 
     preg_match_all('/<img.*?(?: |\\t|\\r|\\n)?src=[\'"]?(.+?)[\'"]?(?:(?: |\\t|\\r|\\n)+.*?)?>/sim', $content, $result, PREG_PATTERN_ORDER); 
     return count($result[1]); 
 }
}
函数使用方法:

<?php echo get_post_images_number().'张图片' ?>

wordpress 获取文章内所有图片

将代码插入functions.php

function hui_get_thumbnail( $single=true, $must=true ) {
    global $post;
    $html = '';
    if ( has_post_thumbnail() ) {
        $domsxe = simplexml_load_string(get_the_post_thumbnail());
        $src = $domsxe->attributes()->src;
        $src_array = wp_get_attachment_image_src(hui_get_attachment_id_from_src($src), 'thumbnail');
        $html = sprintf('<li><img src="%s" /></li>', $src_array[0]);
    } else {
        $content = $post->post_content;
        preg_match_all('/<img.*?(?: |\\t|\\r|\\n)?src=[\'"]?(.+?)[\'"]?(?:(?: |\\t|\\r|\\n)+.*?)?>/sim', $content, $strResult, PREG_PATTERN_ORDER);
        $images = $strResult[1];
        $counter = count($strResult[1]);
        $i = 0;
        foreach($images as $src){
            $i++;
            $src2 = wp_get_attachment_image_src(hui_get_attachment_id_from_src($src), 'thumbnail');
            $src2 = $src2[0];
            if( !$src2 && true ){
                $src = $src;
            }else{
                $src = $src2;
            }
            $item = sprintf('<li><img src="%s" /></li>', $src);
            if( $single){
                return $item;
                break;
            }
            $html .= $item;
            if(
                ($counter >= 4 && $counter < 8 && $i >= 4) ||
                ($counter >= 8 && $i >= 8) ||
                ($counter > 0 && $counter < 4 && $i >= $counter)
            ){
                break;
            }
        }
    }
    return $html;
}
function hui_get_attachment_id_from_src ($link) {
    global $wpdb;
    $link = preg_replace('/-\d+x\d+(?=\.(jpg|jpeg|png|gif)$)/i', '', $link);
    return $wpdb->get_var("SELECT ID FROM {$wpdb->posts} WHERE guid='$link'");
}

以上代码规则可根据自己实际要求来修改

前端调用

<?php echo hui_get_thumbnail(false,true);?>

时间: 2024-08-01 19:41:58

wordpress 获取文章内所有图片个数与图片地址例子的相关文章

wordpress获取文章所有图片实现程序

先来看php获取文章中所有图片正则表达式  代码如下 复制代码 $body="中国WEB第一站<img src=/notfound.jpg>"; preg_match_all("/(src|SRC)=["|'| ]{0,}(http://(.*).(gif|jpg|jpeg|png))/isU",$body,$img_array); $img_array = array_unique($img_array[2]); echo "捕获的

WordPress获取文章内容摘要程序代码

这个是我自己写的获取文章内容摘要程序  代码如下 复制代码 <?php  global $more;    $more = 1;    //1=全文 0=摘要   $my_content = strip_tags(get_the_excerpt(), $post->post_content); //获得文章   $my_content = str_replace(array("rn", "r", "n", " ",

php正则获取文章内容中图片地址例子

 代码如下 复制代码 例子 function getpic($str){  preg_match_all("//isU",$str,$ereg);//正则表达式把图片的整个都获取出来了      $img=$ereg[0][0];//图片     $p="#src=('|\")(.*)('|\")#isU";//正则表达式     preg_match_all ($p, $img, $img1);      $img_path =$img1[2]

巧用WP Keyword Link自动添加WordPress网站文章内链

中介交易 http://www.aliyun.com/zixun/aggregation/6858.html">SEO诊断 淘宝客 云主机 技术大厅 WP Keyword Link是个自动生成关键字內链外链的WordPress插件.既然自动何必还要巧用呢?做链接要是真能全自动就好了.做站群的站长知道自动化操作出来的站点和半自动的区别在于人性化.全靠自动化的机器程序帮你完成链接操作,会出现一大堆访客和你都不想看的内容.有个现成的例子可以看下,百度百科是百度的产品,所以百度搜索名词的时候百科会

WordPress如何获取文章内容页特色图像地址

WordPress获取特色图像地址主要需要用到两个函数get_post_thumbnail_id和wp_get_attachment_image_src.下面是分别获取小.中.大.完整.指定图片规格的图片地址的方法. 获取特色图像:指定100x100 <?php $array_image_url = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), array(100,100)); echo $array_image_

asp正则获取html文件图片地址

Function getIMG(sString)     Dim sReallyDo, regEx, iReallyDo     Dim oMatches, cMatch     '//定义一个空数组     iReallyDo = -1     ReDim aReallyDo(iReallyDo)     If IsNull(sString) Then         getIMG = ""         Exit Function     End If     sReallyDo

html5-将自己网页上的图片指向其他网页的图片地址,显示没有权限

问题描述 将自己网页上的图片指向其他网页的图片地址,显示没有权限 在自己写的jsp代码中,添加了此段代码 out.print("<img src=" + data.getImages().get(1) + "alt=" + data.getImage() + ">"); 显示效果如下: 难道只能将图片下载到本地才有权限访问此图片吗? 解决方案 网页中为什么不显示图片获取网页内所有图片地址网页中图片的显示问题 解决方案二: 人家判断了来

WordPress实现文章关键词自动内链的方法 插件/非插件

关于 WordPress 网站内链的优化的实现方法,有一款很著名的Auto Tags Link插件就可以解决这个问题,但是WordPress不适合过多的使用插件,所以这里才提供了一个代码版的,大家将以下代码添加到当前主题的functions.php文件中就可以了. // WordPress文章关键词自动内链 $match_num_from = 3;    //一个标签少于几次不链接 $match_num_to = 1;    //一个标签最多链接几次 function tag_sort($a, 

WordPress给文章图片自动添加链接

图片自动链接到文章,添加标题和ALT属性 直接将下面的代码添加到主题的 functions.php 文件即可:  代码如下 复制代码 function auto_post_link($content) {  global $post;         $content = preg_replace('/<s*imgs+[^>]*?srcs*=s*('|")(.*?)\1[^>]*?/?s*>/i', "<a href="".get_pe