比较不同模板部件中的值

For certain reasons I have to add a bit of style (margin) to the footer of the page. It should only be applied when the content part of the page is a product archive.

I tried to create a variable in the archive-products.php file like this:

<?php global $template;

$template = 'prodarchive';

var_dump($template);?>

According to the var_dump the value is set. Next I tried to call it in my footer.php like this

<?php if ($template === 'prodarchive' ) {?>

and then the stuff it needs to do.

Some tests shows me that the footer.php file is not able to reproduce the value of $template. What is wrong?

The easiest solution (as pointed out by @seemly) is to apply the body_class() function to the <body> tag:

<body <?php body_class(); ?>>

This will apply a range useful classes that you can use for CSS, including one of the product archive pages.

Another approach is to use is_post_type_archive('product'), which returns a boolean value.

As to why your global variable isn't working in footer.php, I'm pretty sure get_footer() isn't passed all global variables when it's called.

Is the post type not applied as a css class to the body tag?

If not, you can add it to the array of classes that get applied to the body tag, and base any additional styling from that.

How to add classes to body tag