Last active
September 19, 2020 15:34
-
-
Save gasatrya/9855707 to your computer and use it in GitHub Desktop.
Custom slideshow gallery format
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Get the [gallery] shortcode from the post content and display it on index page. It require | |
* gallery ids [gallery ids=1,2,3,4] to display it as thumbnail slideshow. If no ids exist it | |
* just display it as standard [gallery] markup. | |
* | |
* If no [gallery] shortcode found in the post content, get the attached images to the post and | |
* display it as slideshow. | |
* | |
* @since 2.1 | |
* @access public | |
* @uses get_post_gallery() to get the gallery in the post content. | |
* @uses wp_get_attachment_image() to get the HTML image. | |
* @uses get_children() to get the attached images if no [gallery] found in the post content. | |
* @return string | |
*/ | |
function satu_get_format_gallery() { | |
global $post; | |
/* Don't display it on single post. */ | |
if ( is_singular() ) | |
return; | |
/* Set up a default, empty $html variable. */ | |
$html = ''; | |
/* Check the [gallery] shortcode in post content. */ | |
$gallery = get_post_gallery( $post->ID, false ); | |
/* Check if the [gallery] exist. */ | |
if ( $gallery ) { | |
/* Check if the gallery ids exist. */ | |
if ( isset( $gallery['ids'] ) ) { | |
/* Get the gallery ids and convert it into array. */ | |
$ids = explode( ',', $gallery['ids'] ); | |
/* Display the gallery in a cool slideshow on index page. */ | |
$html = '<ul class="rslides">'; | |
foreach( $ids as $id ) { | |
$html .= '<li>'; | |
$html .= wp_get_attachment_image( $id, 'satu-featured' ); | |
$html .= '</li>'; | |
} | |
$html .= '</ul>'; | |
} else { | |
/* If gallery ids not exist, display the standard gallery markup. */ | |
$html = get_post_gallery( $post->ID ); | |
} | |
/* If no [gallery] in post content, get attached images to the post. */ | |
} else { | |
/* Set up default arguments. */ | |
$defaults = array( | |
'order' => 'ASC', | |
'post_type' => 'attachment', | |
'post_parent' => $post->ID, | |
'post_mime_type' => 'image', | |
'numberposts' => -1 | |
); | |
/* Retrieves attachments from the post. */ | |
$attachments = get_children( apply_filters( 'satu_gallery_format_args', $defaults ) ); | |
/* Check if attachments exist. */ | |
if ( $attachments ) { | |
/* Display the attachment images in a cool slideshow on index page. */ | |
$html = '<ul class="rslides">'; | |
foreach ( $attachments as $attachment ) { | |
$html .= '<li>'; | |
$html .= wp_get_attachment_image( $attachment->ID, 'satu-featured' ); | |
$html .= '</li>'; | |
} | |
$html .= '</ul>'; | |
} | |
} | |
/* Return the gallery images. */ | |
return $html; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment