• Hi, I’m trying to get the category (on an archive page) to pass it into the DFP ad tag, but the code I have is not working:

    <script type='text/javascript'>
    	<?php $category = get_the_category();
    		echo 'googletag.pubads().setTargeting("category", "'.$category[0]->slug.'");'; ?>
    	googletag.cmd.push(function() { googletag.display('div-xxxxxx'); });
    </script>

    The code seems to run ok, but the category ‘value’ is not being passed through. It’s always blank.
    Any thoughts?

Viewing 3 replies - 1 through 3 (of 3 total)
  • Moderator bcworkz

    (@bcworkz)

    Where is your code? If outside of the loop, you need to provide a post ID in order to get a value. You might try using category_description(). Wrap your code in a is_category() conditional if the template is used for other types of archives. If you don’t you’ll get a fatal error for non-category queries.

    Thread Starter ViscoDesign

    (@viscodesign)

    Thanks bcworkz, it is outside the loop, on the archive.php page.
    I’ve wrapped the code in is_category, but I’m still trying to figure out how to pass in the ID. It has to be dynamically inserted.

    Thread Starter ViscoDesign

    (@viscodesign)

    Ok, I managed to get it working. This code will insert the category slug from an archive page, into a DFP tag…

    <?php if (is_category()): ?>
    	<script type='text/javascript'>
    		<?php $cat = get_category( get_query_var( 'cat' ) ); $cat_slug = $cat->slug; ?>
    		googletag.pubads().setTargeting("category", <?php echo '"'.$cat_slug.'"'; ?>);
    		googletag.cmd.push(function() { googletag.display('div-xxxxxx'); });
    	</script>
    <?php endif; ?>

    But it’s still not pulling in the assigned DFP ad. Oh well, one step closer I guess.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘DFP custom targeting for categories’ is closed to new replies.