「自学哈网」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代码配合图片显示。
本站声明:
本站所有资源来源于网络,分享目的仅供大家学习和交流!如若本站内容侵犯了原著者的合法权益,可联系邮箱976157886@qq.com进行删除。
自学哈专注于免费提供最新的分享知识、网络教程、网络技术的资源分享平台,好资源不私藏,大家一起分享!
自学哈网 » 「自学哈网」WordPress链接到文章的图片附件自动显示在文章中
本站所有资源来源于网络,分享目的仅供大家学习和交流!如若本站内容侵犯了原著者的合法权益,可联系邮箱976157886@qq.com进行删除。
自学哈专注于免费提供最新的分享知识、网络教程、网络技术的资源分享平台,好资源不私藏,大家一起分享!
自学哈网 » 「自学哈网」WordPress链接到文章的图片附件自动显示在文章中
也想出现在这里?
联系我们吧