How can I import images as a HTML list in Wordpress?
My question is very simple: Is it possible to import many images at once in a post (such as the gallery does) but in开发者_开发知识库side a <ul>
for instance ?
The gallery import is, I think, the only way to import a group of images inside a post, but I can't handle the HTML in this case.
I can be wrong, but I didn't find anything about that. Thank you for your help.
You can insert your images in the gallery without importing it into your post. Then, in the single post template, you can query the images attached to the post to display them.
Attachments are just posts attached to a post, so let’s use get_posts():
function get_post_images($post) {
$args = array(
'post_type' => 'attachment', // Images attached to the post
'numberposts' => -1, // Get all attachments
'post_status' => null, // I don’t care about status for gallery images
'post_parent' => $post->ID, // The parent post
'exclude' => get_post_thumbnail_id($post->ID), // Exclude the thumbnail if you want it on your article list but not inside an article
'post_mime_type' => 'image', // The attachment type
'order' => 'ASC',
'orderby' => 'menu_order ID', // Order by menu_order then by ID
);
return get_posts($args);
}
I suggest to place this function in the functions.php file.
In your single.php file:
<ul>
<?php
$images = get_post_images($post);
foreach ($images as $image):
?>
<li><?php echo wp_get_attachment_image($image->ID, 'medium'); ?></li>
<?php endforeach; ?>
</ul>
WP Codex links:
- get_posts()
- Query parameters
精彩评论