Viewing 10 posts - 1 through 10 (of 10 total)
  • Author
    Posts
  • #1474606

    Hey, I’m trying to get this logo to switch to a different version below 1024px browser width. I’ve been using the filters from https://kriesi.at/support/topic/different-logo-in-the-header-for-mobile-version/ including the addition from @ismael for the screen width support https://kriesi.at/support/topic/different-logo-in-the-header-for-mobile-version/#post-965244 — The logo is switching for mobile but only for iPad mini and below (< 768px).

    My situation may be further complicated because this is a Left Sidebar layout and I’m using some css to switch to the Top Header layout below 1024px.

    So, here’s the code I’m using and the css below that — can you help me get the logo switching at 1024 so the big vertical logo doesn’t show on iPad Air and Pro? Thanks and lmk if you have any questions.

    /* USE ALTERNATE LOGO FOR MOBILE HEADERS */
    add_filter('avf_logo','av_change_logo'); 
    function av_change_logo($logo) { 
    	if(wp_is_mobile()) { 
    	    $logo = "https://deserttortoise.org/wp-content/uploads/desert-tortoise-council-50th-ann-logo-hor.png"; 
    	} 
    	return $logo; 
    }
    add_action('wp_footer', 'ava_custom_script');
    function ava_custom_script(){
    	?>
    	<script type="text/javascript">
    		(function($)) {
    			$(document).ready( function() {
    				if($(window).innerWidth() <= 1024){
    					$('.logo img').attr('src', 'https://deserttortoise.org/wp-content/uploads/desert-tortoise-council-50th-ann-logo-hor.png');
    				}
    			});
    		})(jQuery);
    	</script>
    	<?php
    }
    /* SWITCH LEFT COLUMN TO TOP HEADER AT 1024 */
    @media only screen and (min-width: 768px) and (max-width: 1024px) {
    	.html_header_sidebar .av-sidebar-social-container {
    		display: none;
    	}
    	.responsive #top .av_header_transparency.av_alternate_logo_active .logo a > img {
    		opacity:1
    	}
    	.responsive #top .av_header_transparency .logo img.alternate {
    		display:none;
    	}
    	.responsive #top #wrap_all #header {
    		position: relative;
    		width:100%;
    		float:none;
    		height:auto;
    		margin:0 !important;
    		opacity: 1;
    		min-height:0;
    	}
    	.responsive #top #main {
    		padding-top:0 !important;
    		margin:0;
    	}
    	#header .avia-custom-sidebar-widget-area {
    		display:none;
    	}
    	.responsive.html_header_sidebar .logo {
    		padding: inherit;
    	}
    	.html_header_sidebar .logo img {
    		padding: 10px 10px 10px 30px;
    	}
    	#top #header .av-main-nav > li {
    		display: none;
    	}
    	#top #header .av-main-nav > li#menu-item-search {
    		display: block;
    	}
    	#top #header .av-main-nav > li.av-burger-menu-main.menu-item-avia-special {
    		display: block;
    	}
    	.html_header_sidebar .main_menu {
    		position: absolute;
    		margin: 6%;
    	}
    	.html_header_sidebar #header .av-main-nav {
    		padding: 0;
    	}
    	.html_header_sidebar .logo {
    		width: 40%;
    	}
    }
    @media only screen and (max-width: 1024px) {
    	#search-3.widget, #custom_post_widget-2.widget {
    		padding: 0 40px 40px 40px;
    		display: none;
    	}
    }
    @media only screen and (max-width: 1140px) {
    	.avia-button.avia-size-small {
    		padding: 10px 10px 8px;
    		font-size: 13px;
    		min-width: 65px;
    	}
    }  
    @media print {
        .html_header_left #top #header { 
    		display: none; 
    	}
        .html_header_left #main {
    		margin-left: 0;
    	}
    }
    @media only screen and (min-device-width: 1024px) and (max-device-width: 1366px) and (-webkit-min-device-pixel-ratio: 2) and (orientation: landscape)  {
    	#search-4, #custom_post_widget-7 {
    		display:none;
    	}
    }
    #1474607

    The if(wp_is_mobile()) { is a check if you are watching it on a mobile device ! : Link

    is that what you try to do – or do you always want to change the logo if the header turns to header top posiiton – even for desktop screens lower than a given value (1024px)

    this maybe a quick and dirty way – and please test if it works on other browsers – and mobile browsers too:
    ( in that case – you do not need those functions.php entries – but i will look if there is a solution for that too)

    @media only screen and (max-width: 1024px) {
        .av-logo-container img {
            content: url('https://deserttortoise.org/wp-content/uploads/desert-tortoise-council-50th-ann-logo-hor.png');
        }
    }
    #1474608

    can you check first that custom script and replace with:
    the console reports an error in the script ( looks like there is a superfluous round bracket )

    function ava_custom_script(){
    ?>
    <script type="text/javascript">
    window.addEventListener("DOMContentLoaded", function () { 
    	(function($){	
    		if($(window).innerWidth() <= 1024){
    			$('.logo img').attr('src', 'https://deserttortoise.org/wp-content/uploads/desert-tortoise-council-50th-ann-logo-hor.png');
    		}
    	})(jQuery);
    });
    </script>
    <?php
    }
    add_action('wp_footer', 'ava_custom_script');

    but : i guess we had to find a way – where a resize might be included.

    #1474609

    Can you please remove the relevant snippets from your functions.php (save them to your desktop) and insert this instead?

    function change_logo_with_screenwidth(){
    ?>
    <script>
    window.addEventListener("DOMContentLoaded", function () { 
    	(function($){
    		
    		// this is for loading the page on smaller screen-width 
    		if ($(window).width() < 1025) {
    			$('.logo img').attr('src', 'https://deserttortoise.org/wp-content/uploads/desert-tortoise-council-50th-ann-logo-hor.png');
    		}
    		else {
    			$('.logo img').attr('src', 'https://deserttortoise.org/wp-content/uploads/desert-tortoise-council-50th-ann-logo.png');
    		}
    
    		function b() {
    			if ($(window).width() < 1025) {
    				$('.logo img').attr('src', 'https://deserttortoise.org/wp-content/uploads/desert-tortoise-council-50th-ann-logo-hor.png');
    			}
    			else {
    				$('.logo img').attr('src', 'https://deserttortoise.org/wp-content/uploads/desert-tortoise-council-50th-ann-logo.png');
    			}
    		}
    
    		$(window).on('debouncedresize', function() {
    			b();
    		});
    
    	})(jQuery);
    });
    </script>
    <?php
    }
    add_action('wp_footer', 'change_logo_with_screenwidth');

    we had to see if the srcset inserted images will follow that script …

    #1474610

    sadly – i tested it on a working page and the responsive images option does disturb that change :
    see screenshot on less than 1025px :

    you see that it is replaced – but the srcset is untouched on that. – so this is not the solution!

    #1474612

    hm – that works best till now:
    :lol

    @media only screen and (max-width: 1024px) {
        .av-logo-container img {
            content: url('https://deserttortoise.org/wp-content/uploads/desert-tortoise-council-50th-ann-logo-hor.png');
        }
    }
    #1474643

    Günter! Thanks, once again, for all your work, time and attention to detail! — that quick and dirty css seems to do the trick — I’m surprised and a little embarrassed I didn’t try that myself! :). I’ve removed the snippets from my functions.php file. I wonder if anyone on the Enfold team would raise a flag on this method, or why I’m not finding it in their solutions / they seem to be promoting the php method.

    #1474644

    if this also works on mobile devices (I remember that it didn’t work with older browsers), then it is probably the best performing solution. Because a script solution would have to constantly query the screen width, which is why a “debounce and resize” function would have to be used for performance reasons. Enfold itself provides such a function. (Enfold’s debouncedresize function is a performance-optimized way to handle resize events without triggering excessive function calls)

    function custom_logo_for_mobile(){
    ?>
    <script>
    (function($) {
        function updateLogo() {
            var $logo = $('.logo img'); // Select the logo image
            var mobileLogo 	= "/wp-content/uploads/desert-tortoise-council-50th-ann-logo-hor.webp";
            var defaultLogo = "/wp-content/uploads/desert-tortoise-council-50th-ann-logo.webp";
    
            if ($(window).width() <= 1024) {
                $logo.attr('src', mobileLogo); // Use mobile logo
            } else {
                $logo.attr('src', defaultLogo); // Use default logo
            }
        }
    
        // Trigger logo update on debouncedresize
        $(window).on('debouncedresize', function() {
            updateLogo();
        });
    
        // Run logo update on initial load
        $(document).ready(function() {
            updateLogo();
        });
    })(jQuery);
    </script>
    <?php
    }
    add_action('wp_footer', 'custom_logo_for_mobile', 20 );

    so this works too – but the srcset originally generated (during loading) remains and is then also displayed.
    I don’t know how to change the srcset as well. If I switch off the responsive images under Performance, the change of logo images is also displayed at the frontend.

    #1474691

    Thanks again for the detailed explanation. Looks like the css “content” property is pretty well supported — https://caniuse.com/mdn-css_properties_content — good enough for me, I think! :)

    #1474699

    Hi,

    Great! Glad to know that this has been resolved. Thanks to @Guenni007.

    Best regards,
    Ismael

Viewing 10 posts - 1 through 10 (of 10 total)
  • You must be logged in to reply to this topic.