Created
February 25, 2022 15:14
-
-
Save florianziegler/bdd65a04a44f013fedc60ca8b9df27d2 to your computer and use it in GitHub Desktop.
Change the output of the picu_collection_list shortcode to include a random images as thumbnail
This file contains hidden or 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
/** | |
* Change the output of the picu_collection_list shortcode | |
* | |
* @param string $collection_list The original html output | |
* @param object $query The query of returned collections | |
* @param array $atts The shortcode attributes | |
* @param string $content The content between the shortcode tags | |
*/ | |
function my_picu_collection_list( $collection_list, $query, $atts, $content ) { | |
if ( $query->have_posts() ) { | |
$collection_list = '<ul class="picu-collection-list">'; | |
while ( $query->have_posts() ) { | |
$query->the_post(); | |
$collection_list .= '<li class="picu-status-' . get_post_status() . '"><a href="' . get_permalink() . '">'; | |
$picu_collection_gallery_ids = get_post_meta( get_the_ID(), '_picu_collection_gallery_ids', true ); | |
$image_ids = explode( ',', $picu_collection_gallery_ids ); | |
shuffle( $image_ids ); | |
$image = wp_get_attachment_image( $image_ids[0], 'picu_thumbnail' ); | |
$collection_list .= $image; | |
$collection_list .= get_the_title() . '</a></li>'; | |
} | |
wp_reset_query(); | |
$collection_list .= '</ul>'; | |
} | |
else { | |
$collection_list = '<div class="picu-no-collections">' . $content . '</div>'; | |
} | |
return $collection_list; | |
} | |
add_filter( 'picu_collection_list', 'my_picu_collection_list', 10, 4 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment