Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save MasterHans/5ac2e7d7bc435c981e09 to your computer and use it in GitHub Desktop.
Save MasterHans/5ac2e7d7bc435c981e09 to your computer and use it in GitHub Desktop.
// The WordPress Loop
<?php if ( have_posts ) : ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php if ( 'gallery' == get_post_format( get_the_ID() ) ) : ?>
<!-- Layout your gallery here -->
<?php else : ?>
<!-- Layout your standard posts here -->
<?php endif; ?>
<?php endwhile; ?>
<?php else : ?>
<!-- No posts were found -->
<?php endif; ?>
<?php
/* The loop */
while ( have_posts() ) : the_post();
if ( get_post_gallery() ) :
$gallery = get_post_gallery( get_the_ID(), false );
/* Loop through all the image and output them one by one */
foreach( $gallery['src'] as $src ) : ?>
<img src="<?php echo $src; ?>" class="my-custom-class" alt="Gallery image" />
<?php
endforeach;
endif;
endwhile;
?>
<?php
//получаем все картинки из галлереи поста в виде массива
$gallery = get_post_gallery( get_the_ID(), false );
$ids_images = explode(',', $gallery['ids']);
//получаем объект $attachments из медиа библиотеки по ID в виде массива где ключом является ключ массива $ids_images
$args = array( 'include' => $gallery['ids'], 'post_mime_type' => 'image', 'post_type' => 'attachment', 'post_parent' => null, 'orderby' => 'name', 'order' => 'ASC');
$attachments = get_children($args);
$gallery_attachments_ids = [];
foreach( $attachments as $attachment) {
if (!empty($attachment->guid)) {
$gallery_attachments_ids [array_search($attachment->ID, $ids_images)] = $attachment->ID;
};
};
//сортируем по ключу (в том порядке в котором картинки идут в медиатеке)
ksort($gallery_attachments_ids);
// echo get_post_meta($gallery_attachments_ids[0], '_wp_attachment_image_alt', true);
foreach( $gallery_attachments_ids as $gallery_attachment_id) { ?>
<li><a href="<?php echo wp_get_attachment_image_url($gallery_attachment_id, [770,500]); ?>" title=""><img src="<?php echo wp_get_attachment_image_url($gallery_attachment_id, [74,67]); ?>" alt="" title="<?php echo get_the_title($gallery_attachment_id) ?>"/></a></li>
<?php }; ?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment