Skip to content
Advertisement

WordPress add script to footer for specific category (archive and posts)

i am trying to add a google ad script for specific category only. The code should be and in the archives page for that category and the posts of that category.

Here is the code I am using in my functions.php file

add_action('wp_footer','MY_FUNCTION');
function MY_FUNCTION(){
   if ( is_category(309) ){
    echo '<script>the script</script>';
}
?>

but for some reason it breaks the site

Advertisement

Answer

Use include instead of echo. Store the Script in a separate php file and the call that file via include.

it won’t break the site.

For examble,

create a script.php file and add the script. The call the file via include.

add_action('wp_footer','MY_FUNCTION');
function MY_FUNCTION(){
if ( is_category(309) ){
include 'script.php';}
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement