美文网首页
调用文章内部所有图片

调用文章内部所有图片

作者: Stonesy | 来源:发表于2020-04-21 12:31 被阅读0次

1.找到Appearance-->Theme Editor-->functions.php 加入以下代码

function get_all_image() {
global $post;
$first_img = '';
ob_start();
ob_end_clean();
$output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $matches,PREG_SET_ORDER);
return $matches[0][0];//文章里面所有图片  String
return $matches[1][0];//文章里面所有图片  String
}

2.在文章页面调用

.list{
            margin:6% auto;
            width:96%;
            max-width:1200px;
            overflow:hidden;
            list-style:none;
        }
        .list li{
            float:left;
            width:30%;
            margin-left:3.3%;
        }
        .list li:nth-child(3n+1){
            margin-left:0;
        }
        .list li a{
            display:block;
        }
        .list li a span{
            ext-overflow:ellipsis; 
            display: -webkit-box;
            -webkit-box-orient: vertical;
            -webkit-line-clamp: 1;
            overflow: hidden;
            margin-bottom:2%;
        }
        .list li a p {
            ext-overflow:ellipsis; 
            display: -webkit-box;
            -webkit-box-orient: vertical;
            -webkit-line-clamp: 3;
            overflow: hidden;
            text-align:justify;
            margin-bottom: 12px;
        }
        .list li a img{
            width:32.1%;
            margin-bottom:2%;
            height:auto;
            display:none;
        }
            .list li a img:first-child{
                width:100%;
                display:block;
            }
            .list li a img:first-child+img,
            .list li a img:first-child+img+img,
            .list li a img:first-child+img+img+img{
                display:inline-block;
            }
<ul class="list">
<?php
$args = array( 'numberposts' => 5, 'orderby' => 'rand', 'post_status' => 'publish' );
$rand_posts = get_posts( $args );
foreach( $rand_posts as $post ) : ?>
   <li>
   <a href="<?php the_permalink(); ?>">
   <?php the_excerpt();?>
       <div class="img_box"><?php echo get_all_image()?></div>
   </a>
   </li>
<?php endforeach; ?>
  </ul>

相关文章

  • 调用文章内部所有图片

    1.找到Appearance-->Theme Editor-->functions.php 加入以下代码 2.在...

  • SDWebImage源码分析

    SDWebImage介绍 UIView+WebCache类别: 所有设置图片的方法内部都会且仅调用此方法 UIVi...

  • pprof

    top flat 函数内部除去调用其他函数的开销cum 函数内部所有开销(包括调用其他人)sum 本行以及本行之上...

  • php获取文章的第一张图片

    以上代码默认调用文章首张图片,当文章没有图片的时候,随机调用主题style/images/random/下的1.j...

  • Android WebView 支持图片上传及照相beta

    Android WebView内部支持图片上传,调用默认界面,也可能自定义布局文件显示dialog HTML标签 ...

  • jquery ajax 之 jqXHR 和 Data Types

    jQuery 发送的所有 Ajax 请求,内部都会通过调用 $.ajax()函数来实现。通常没有必要直接调用这个函...

  • RxJava 创建操作符

    ReactiveX 系列文章目录 just 内部触发对 Observer 的 onNext 方法的调用,just ...

  • 函数

    定义 调用 arguments js函数内部有一个默认的关键字 arguments,它指向当前函数调用者传入所有的...

  • 函数的递归(1)

    函数内部自身调用自身的编程技巧 就叫递归一. 递归函数的特点 一个函数 内部 自己调用自己-- 函数内部可以调用其...

  • 【Solidity学习笔记】外部函数的调用

    3.8 外部函数的调用 在Solidity中,有两种函数调用:内部函数调用和外部函数调用。内部函数调用是指一个函数...

网友评论

      本文标题:调用文章内部所有图片

      本文链接:https://www.haomeiwen.com/subject/hfluihtx.html