Created
September 27, 2021 10:40
-
-
Save kimcoleman/fb8737ce49ec6649c4cf9c6181dc9d7e to your computer and use it in GitHub Desktop.
Shortcode to show a grid of all demo sites with featured image in the network.
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
<?php | |
/** | |
* Shortcode to show a grid of all demo sites with featured image in the network. | |
* | |
*/ | |
function demo_sites_grid_shortcode( ) { | |
ob_start(); | |
$subsites = get_sites(); | |
if ( ! empty ( $subsites ) ) { | |
echo '<div class="wp-block-columns">'; | |
foreach( $subsites as $subsite ) { | |
$subsite_id = get_object_vars( $subsite )["blog_id"]; | |
if ( $subsite_id === '1' ) { | |
continue; | |
} | |
switch_to_blog( $subsite_id ); | |
$frontpage_id = get_option( 'page_on_front' ); | |
if ( has_post_thumbnail( $frontpage_id ) ) { ?> | |
<div class="wp-block-column" style="flex-basis:50%"> | |
<a href="<?php echo esc_url( home_url( '/' ) ); ?>" title="<?php bloginfo( 'name' ); ?>"> | |
<?php | |
echo get_the_post_thumbnail( $frontpage_id, 'large' ); | |
echo '<p class="text-center">' . get_post_meta( get_post_thumbnail_id( $frontpage_id ), '_wp_attachment_image_alt', true ) . '</p>'; | |
?> | |
</a> | |
</div> <!-- end wp-block-column --> | |
<?php | |
} | |
restore_current_blog(); | |
} | |
echo '</div> <!-- end wp-block-columns -->'; | |
} | |
$temp_content = ob_get_contents(); | |
ob_end_clean(); | |
return $temp_content; | |
} | |
add_shortcode( 'demo_sites_grid', 'demo_sites_grid_shortcode' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment