下面这段代码可以将链接到文章的图片附件自动显示在文章中,不需要你手工编辑。
将代码添加到你的主题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代码配合图片显示。