|
<?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; |
|
|
|
} |