当前位置: 首页 > 阿里云优惠 > 阿里云服务器优惠>> WordPress链接到文章的图片附件自动显示在文章中 – WordPress 教程

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


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="https://xyy1.com/<?php echo $full[0]; ?>" target="_blank"><img src="https://xyy1.com/<?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="https://xyy1.com/27306.html' . $url . '">' . $author . '</a></span> ';            } ?>            </div>  <?php }  add_filter('the_content', 'usp_display_images');  

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


服务器购买/咨询热线:1563700917115617636856

本文链接:http://www.sqxnmj.com/alfwq/28343.html
tags:
腾讯云服务器特惠

  • 关注微信
是否收录:

猜你喜欢

微信公众号