Based on the comments I think I can walk you through this.
If you want to “inject” code like this into your theme, one of the better ways is with WordPress Action API.
Since what you’re trying to get on your site is Javascript, we’ll hook into the footer. Assuming your theme is built properly, adding this to your functions.php will add some code to the bottom of your site, on all pages:
add_action('wp_footer', function(){
echo "<mark>👋 Hello</mark>";
});
If we then add in your conditionals, we can make that message only show on your categories page. See if the Hello message is on the page you want, and shouldn’t be on pages you don’t want.
add_action('wp_footer', function(){
if (is_category(10) || cat_is_ancestor_of(10, get_query_var('cat')))
echo "<mark>👋 Hello</mark>";
});
If that didn’t work, is_category()
is just arguing against $wp_query
. So we can review the $wp_query
for the page you’re after, to ensure you’re conditionalizing the right things. Visit the page where the logo should change, and see what the cat
value is:
add_action('wp_footer', function(){
global $wp_query;
echo "<pre>".print_r($wp_query,true)."</pre>";
//if (is_category(10) || cat_is_ancestor_of(10, get_query_var('cat')))
// echo "<mark>👋 Hello</mark>";
});
If it did work, or you resolved why it didn’t, we’ll we’re golden. You conditionals are fine. Now it’s just a matter of adding your script:
add_action('wp_footer', function(){
if (is_category(10) || cat_is_ancestor_of(10, get_query_var('cat'))) {
?>
<script>
document.getElementsByClassName('logo')[0].src="https://wordpress.stackexchange.com/questions/290705/adventurelogo.png";
</script>
<?php
}
});
If that doesn’t work, but the Hello message did, your issue is with your Javascript code. And well, that’s probably it too, looking closer at your JS code, you’re using the logo as a relative path – which won’t work if you’re using named permalinks. You need an image src with the full image URL. Assuming adventurelogo.png
is in the root of your theme, it’d be more like:
add_action('wp_footer', function(){
if (is_category(10) || cat_is_ancestor_of(10, get_query_var('cat'))) {
?>
<script>
var newLogo = '<?= get_template_directory_uri() ?>/adventurelogo.png';
document.getElementsByClassName('logo')[0].src = newLogo
</script>
<?php
}
});