-
danstramer
Hi,
I would like to replace the header logo on a specific page, but only when a logo is uploaded to a custom field (ACF).
So I would need a mesh of 2 functions:// 1. The GP filter for replacing the logo: add_filter( 'generate_logo', function( $logo ) { // Return our category logo URL if ( is_page( 42 ) ) { return 'URL TO YOUR PAGE LOGO'; } // Otherwise, return our default logo return $logo; } ); // 2. Checking if an image exists in the custom field and then replacing it within the filter: $other_logo = get_field('other_logo'); $size = 'full'; if($other_logo):
All of this need to be placed within functions.php.
And I would need it to work for the sticky header too 🙂
Could you help me out?
Thanks!
Dan -
Fernando
Hi Dan,
Can you try this?:
add_filter( 'generate_logo', function( $logo ) { $attachment_id = get_field('other_logo'); $size = "medium"; // (thumbnail, medium, large, full or custom size) if ( is_page( 42 ) && isset($attachment_id) ) { $image = wp_get_attachment_image_src( $attachment_id, $size ); return $image; } // Otherwise, return our default logo return $logo; } );
-
danstramer
Thanks Fernando.
Adding this code removes the<div class="site-logo">...</div>
container.
Also, I would like the condition to check just if an image was added via custom field, not for a specific page.Thanks
Dan -
danstramer
Hi Fernando,
Any update?Thanks
Dan -
David
Hi there,
try this:
add_filter( 'generate_logo', function( $logo ) { $alt_attachment_id = get_field('other_logo'); $alt_image_size = "medium"; // (thumbnail, medium, large, full or custom size) $alt_image = wp_get_attachment_image_src( $alt_attachment_id, $alt_image_size ); if ( $alt_image ) { $logo = $alt_image; } return $logo; } );
-
danstramer
Hi David,
I’ve applied the code, but still no go. the ‘site-logo’ container is not showing up.
I added access in the private information section.
added web access too
Thanks for your help.Dan
-
Hey Dan,
In ACF, try setting the “Return Format” for the image to “Image URL”.
Let us know 🙂
-
danstramer
Hey Tom,
I’ve changed ACF’s return format to “Image URL” – now it’s showing the original logo, not the one added via the custom field.Thanks,
Dan -
Try this one instead:
add_filter( 'generate_logo', function( $logo_url ) { $alt_attachment = get_field('other_logo'); if ( $alt_attachment) { $logo_url = $alt_attachment; } return $logo_url; } );
-
danstramer
Thanks Ying, it’s replacing the logo, but not the ‘Sticky logo’ – any advice for that?
Dan
-
Try this filter for sticky navigation logo:
add_filter( 'generate_sticky_navigation_logo_output', 'lh_change_sticky_navigation_logo_output', 10, 2 ); function lh_change_sticky_navigation_logo_output( $logo ) { $alt_logo_url = get_field( 'other_logo' ); if ( $alt_logo_url ) { return sprintf( '<div class="sticky-navigation-logo"> <a href="%1$s" title="%2$s" rel="home"> <img src="%3$s" class="is-logo-image" alt="%2$s" width="100px" height="100px" /> </a> </div>', esc_url( apply_filters( 'generate_logo_href', home_url( '/' ) ) ), esc_attr( apply_filters( 'generate_logo_title', get_bloginfo( 'name', 'display' ) ) ), esc_url( $alt_logo_url ), ); } return $logo; }
-
danstramer
Thank you Ying, that solved it.
Dan
-
Glad to hear that 🙂
- You must be logged in to reply to this topic.