显示所有链接到文章的图片附件

WordPress4599阅读模式
摘要下面这段代码可以将链接到文章的图片附件自动显示在文章中,不需要你手工编辑。 将代码添加到你的主题functions.php文件中 当然,还需另外的CSS样式及HTML代码配合图片显...

下面这段代码可以将链接到文章的图片附件自动显示在文章中,不需要你手工编辑。

将代码添加到你的主题functions.php文件中:

  1. // display user-submitted images  
  2. function usp_display_images() {  
  3.      global $post;  
  4.      $args = array('order'=>'ASC', 'post_type'=>'attachment', 'post_parent'=>$post->ID, 'post_mime_type'=>'image', 'post_status'=>null);  
  5.      $items = get_posts($args); ?>  
  6.    
  7.           <div class="usp-image">  
  8.           <?php foreach ($items as $item) {  
  9.                $atts = wp_get_attachment_image_src($item->ID, 'medium');  
  10.                $full = wp_get_attachment_image_src($item->ID, 'full'); ?>  
  11.                <a href="<?php echo $full[0]; ?>" target="_blank"><img src="<?php echo $atts[0]; ?>" width="<?php echo $atts[1]; ?>" height="<?php echo $atts[2]; ?>" alt=""></a>  
  12.           <?php }  
  13.                $author = get_post_meta($post->ID, 'user_submit_name', true);  
  14.                $url = get_post_meta($post->ID, 'user_submit_url', true);  
  15.                if (filter_var($url, FILTER_VALIDATE_URL) !== false) {  
  16.                echo '<span class="usp-author-link">Posted by <a href="' . $url . '">' . $author . '</a></span> ';  
  17.           } ?>  
  18.           </div>  
  19.    
  20. <?php }  
  21. add_filter('the_content', 'usp_display_images');  

当然,还需另外的CSS样式及HTML代码配合图片显示。

原文:http://wp-mix.com/display-images-attached-post/

本站文章大部分始于原创,用于个人学习记录,可能对您有所帮助,仅供参考!

weinxin
我的微信
版权声明
本站原创文章转载请注明文章出处及链接,谢谢合作!
 
知更鸟
评论  4  访客  4
    • 前端观察站
      前端观察站 1

      貌似这个可以用来做轮播图什么的效果啊,不错!

      • 郑州包装设计
        郑州包装设计 2

        不错!先收藏了,到时候会用到的

        • 大豆异黄酮
          大豆异黄酮 3

          看见代码就头疼

          • 图书资讯
            图书资讯 2

            鸟哥你好,帮忙看看我这个网站:http://smszdk.com/blog/ 文章里面都有引用一些图片,就是无法显示在首页的轮播图片和缩小图里面,求妙方!谢谢。

          匿名

          发表评论

          匿名网友
          :?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

          拖动滑块以完成验证