Skip to content

Instantly share code, notes, and snippets.

@ControlledChaos
Last active August 25, 2019 23:59
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ControlledChaos/5badcc3c3ecf86ed6e129cac7f7a7243 to your computer and use it in GitHub Desktop.
Save ControlledChaos/5badcc3c3ecf86ed6e129cac7f7a7243 to your computer and use it in GitHub Desktop.
Display a random logo image from an ACF gallery field.

Random ACF Logo w/ Customizer Fallback

Displays a random logo image from a gallery field. Falls back first to the logo in the Customizer, then to a theme default file.

Requirements

  • Requires Advanced Custom Fields Pro to be active to display a random inage.
  • Set the ACF gallery to return the URL of images.
  • Use in a template as <?php ccd_random_logo(); ?>

Notes

  • Change the function prefix as needed.
  • Change the default theme image directory/file as needed.
  • Change the theme text domain for the image alt attribute.
<?php
/**
* Random logo
*
* Displays a random logo image from the gallery on
* the theme settings page.
*
* Requires Advanced Custom Fields Pro to be active.
* Falls back first to the logo in the Customizer,
* then to a theme default file.
*
* Set the ACF gallery to return the URL of images.
*
* Use in a template as `<?php ccd_random_logo(); ?>`
*
* @since 1.0.0
* @access public
* @return string Returns the logo markup.
*/
function ccd_random_logo() {
// Set up the random image if ACF Pro is active.
if ( class_exists( 'acf_pro' ) ) :
/**
* Get fields from the theme options page.
*
* If the gallery is not on an options page,
* for example on the front page edit screen,
* then remove the `option` parameter.
*/
$images = get_field( 'mixes_theme_header_icons', 'option' );
// Shuffle the order of the gallery, if any images.
if ( $images ) {
$random = shuffle( $images );
} else {
$random = null;
}
// Count the number of images in the gallery.
$count = count( $images );
// If there is only one image, return that image.
if ( ! empty( $random ) && 1 == $count ) {
$image = $images[0];
// If there are more than one image, return a random image.
} elseif ( is_array( $images ) && ! empty( $images ) && ! empty( $random ) ) {
$image = $images[$random];
// If there are no images in the gallery but one set in the customizer.
} elseif ( has_custom_logo() ) {
$logo_id = get_theme_mod( 'custom_logo' );
$logo_src = wp_get_attachment_image_src( $logo_id , 'full' );
$image = $logo_src[0];
// Otherwise return the theme default logo image.
} else {
$image = get_theme_file_uri( '/assets/images/logos/default-logo.jpg' );
}
// If ACF Pro is not active.
else :
// If a logo is set in the customizer, get that image.
if ( has_custom_logo() ) {
$logo_id = get_theme_mod( 'custom_logo' );
$logo_src = wp_get_attachment_image_src( $logo_id , 'full' );
$image = $logo_src[0];
// Otherwise return the theme default logo image.
} else {
$image = get_theme_file_uri( '/assets/images/logos/default-logo.jpg' );
}
endif;
// The logo image markup with link to home page.
$logo = sprintf(
'<a href="%1s"><img class="custom-logo" src="%2s" alt="%3s" /></a>',
site_url(),
$image,
esc_attr( get_bloginfo( 'name' ) . esc_html__( ' logo', 'theme-domain' ) )
);
// Return the logo markup.
echo $logo;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment