I have the following javascript code:
$(window).scroll(function() {
if($(this).scrollTop() > 50) /*height in pixels when the navbar becomes non opaque*/
{
$('.navbar-default').addClass('sticky');
$('.navbar-brand img').attr('src','assets/images/logo.png'); //change src
} else {
$('.navbar-default').removeClass('sticky');
$('.navbar-brand img').attr('src','assets/images/logo__footer.png')
}
});
Is it possible to insert a wp custom php code
<?php the_custom_logo(); ?>
Instead of this static attribute
.attr('src','assets/images/logo.png');
Many thanks in advance.
Yes, you can, provided your JavaScript code is embedded into a PHP file.
JavaScript can be used within the WordPress platform to add dynamic elements to pages and posts, or across your entire website.
JavaScript can also talk with your PHP code on the web server whenever it needs to update something (either on the server or on the web page).
You need to set variable in template:
<script>
var logoImage = <?php the_custom_logo(); ?>;
var logoImageFooter = <?php the_custom_logo()?> //here footer logo
</script>
and than, in your js file use it
$(window).scroll(function() {
if($(this).scrollTop() > 50) /*height in pixels when the navbar becomes non opaque*/
{
$('.navbar-default').addClass('sticky');
$('.navbar-brand img').attr('src',logoImage); //change src
} else {
$('.navbar-default').removeClass('sticky');
$('.navbar-brand img').attr('src',logoImageFooter)
}
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With