Created
November 24, 2022 16:20
-
-
Save fredbradley/42c74d6aae62f8a76e805a7621591366 to your computer and use it in GitHub Desktop.
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 | |
new AssetGallery(); | |
class AssetGallery { | |
function __construct() { | |
add_shortcode("latest_assets", array($this, 'shortcode')); | |
} | |
function shortcode($atts) { | |
$a = shortcode_atts(array( | |
'category' => null, | |
'num' => 4, | |
'columns' => 4, | |
), $atts); | |
$args = array( | |
'post_type' => 'attachment', | |
'post_mime_type' => 'image', | |
'post_status' => 'any', | |
'posts_per_page' => $a['num'], | |
); | |
if ($a['category'] !== null): | |
switch($a['category']): | |
case "cubitt": | |
case "east": | |
case "west": | |
case "south": | |
case "north": | |
case "loveday": | |
case "rhodes": | |
case "connaught": | |
// If the category is the name of a house. We assume we only use this on the house mini-sites. | |
// Therefore we only get get only the photos that are marked as "display-on-homepage" as a tag in each mini-site. | |
$args['tax_query'] = [ | |
[ | |
"taxonomy" => "post_tag", | |
"field" => "slug", | |
"terms" => "display-on-homepage" | |
] | |
]; | |
break; | |
// For all other cases... | |
default: | |
$args['tax_query'] = array( | |
"relation" => "OR", | |
array( | |
'taxonomy' => 'post_tag', | |
'field' => 'slug', | |
'terms' => $a['category'], | |
), | |
array( | |
'taxonomy' => 'category', | |
'field' => 'slug', | |
'terms' => $a['category'] | |
) | |
); | |
break; | |
endswitch; | |
endif; | |
$query = new WP_Query($args); | |
$image_ids = array(); | |
foreach($query->posts as $post): | |
$image_ids[] = $post->ID; | |
endforeach; | |
if (count($image_ids) == 0) | |
return false; | |
$list = implode(",", $image_ids); | |
$return = "<div class=\"asset-gallery\">"; | |
$return .= do_shortcode("[gallery link=\"file\" columns=\"".$a['columns']."\" size=\"thumbnail\" orderby=\"rand\" ids=\"".$list."\"]"); | |
$return .= "</div>"; | |
wp_reset_postdata(); | |
wp_reset_query(); | |
return $return; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment