最近访客
百度未收录

WordPress链接到文章的图片附件自动显示在文章中

WordPress链接到文章的图片附件自动显示在文章中-个人心得记录
WordPress

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

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

// display user-submitted images  
function usp_display_images() {  
     global $post;  
     $args = array(\'order\'=>\'ASC\', \'post_type\'=>\'attachment\', \'post_parent\'=>$post->ID, \'post_mime_type\'=>\'image\', \'post_status\'=>null);  
     $items = get_posts($args); ?>  
          <div class=\"usp-image\">  
          <?php foreach ($items as $item) {  
               $atts = wp_get_attachment_image_src($item->ID, \'medium\');  
               $full = wp_get_attachment_image_src($item->ID, \'full\'); ?>  
               <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>  
          <?php }  
               $author = get_post_meta($post->ID, \'user_submit_name\', true);  
               $url = get_post_meta($post->ID, \'user_submit_url\', true);  
               if (filter_var($url, FILTER_VALIDATE_URL) !== false) {  
               echo \'<span class=\"usp-author-link\">Posted by <a href=\"\' . $url . \'\">\' . $author . \'</a></span> \';  
          } ?>  
          </div>  
<?php }  
add_filter(\'the_content\', \'usp_display_images\');  

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

温馨提示:本文最后更新于2022/10/20 03:45:27。若文章内容或图片失效,请留言联系站长反馈!
!
也想出现在这里? 联系我们
创意广告
© 版权声明
THE END
点赞0赞赏 分享
评论 抢沙发

请登录后发表评论

    请登录后查看评论内容