Skip to content

Commit 2851399

Browse files
authored
Merge pull request #87 from INN/86-category-tags-targeting
Fix targeting for categories and tags when wp_footer is outside The Loop
2 parents 937ee77 + 829019f commit 2851399

File tree

2 files changed

+17
-3
lines changed

2 files changed

+17
-3
lines changed

dfw-init.php

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,7 @@ public function footer_script() {
216216
}
217217

218218
private function targeting() {
219+
global $wp_query;
219220
/** @see http://codex.wordpress.org/Conditional_Tags */
220221

221222
$targeting = array();
@@ -263,7 +264,12 @@ private function targeting() {
263264
}
264265

265266
if ( is_singular() && ( ! is_post_type_archive() && ! is_front_page() ) ) {
266-
$cats = get_the_category();
267+
// https://wordpress.org/support/topic/categories-tags-bug-and-solution/
268+
$cat_post_id = $wp_query->get_queried_object_id();
269+
if ( 0 === $cat_post_id ) {
270+
$cat_post_id = null; // get_queried_object_id returns 0 for no post found; get_the_category expects null;
271+
}
272+
$cats = get_the_category( $cat_post_id );
267273
$targeting['Category'] = array();
268274

269275
if ( ! empty( $cats ) ) {
@@ -281,8 +287,13 @@ private function targeting() {
281287
$targeting['Category'][] = $queried_object->slug;
282288
}
283289

284-
if ( is_single() ) {
285-
$tags = get_the_tags();
290+
if ( is_singular() && ( ! is_post_type_archive() && ! is_front_page() ) ) {
291+
// https://wordpress.org/support/topic/categories-tags-bug-and-solution/
292+
$tag_post_id = $wp_query->get_queried_object_id();
293+
if ( 0 === $tag_post_id ) {
294+
$tag_post_id = null; // get_queried_object_id returns 0 for no post found; get_the_tags expects null;
295+
}
296+
$tags = get_the_tags( $tag_post_id );
286297
if ( $tags ) {
287298
$targeting['Tag'] = array();
288299
foreach ( $tags as $t ) {

readme.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,15 @@ For more advanced documentation for developers and advanced users see [the offic
3333

3434
= 0.3 =
3535

36+
Thanks to WordPress.org user [onlineforbusiness](https://wordpress.org/support/users/onlineforbusiness/) and GitHub user [dbeniaminov](https://github.com/dbeniaminov) for their contributions to this release.
37+
3638
- Rebrands from "DoubleClick for WordPress" to "Super Cool Ad Manager Plugin", because [Google merged DoubleClick for Publishers into Google Ad Manager](https://www.blog.google/products/admanager/introducing-google-ad-manager/).
3739
- Updates to `jquery.dfp.js` version 2.4.2, adding `setCentering` support. [PR #67](https://github.com/INN/doubleclick-for-wp/pull/67) for [issue #66](https://github.com/INN/doubleclick-for-wp/issues/66)
3840
- Removes 'single' page targeting from post-type archives and from static front pages. [PR #72](https://github.com/INN/doubleclick-for-wp/pull/72) for [issue #61](https://github.com/INN/doubleclick-for-wp/issues/61), thanks to GitHub user [dbeniaminov](https://github.com/dbeniaminov).
3941
- Adds "Category" targeting on category archive. [PR #72](https://github.com/INN/doubleclick-for-wp/pull/72) for [issue #61](https://github.com/INN/doubleclick-for-wp/issues/61).
4042
- Adds "Tag" targeting on tag archive. [PR #74](https://github.com/INN/doubleclick-for-wp/pull/74) for [issue #29](https://github.com/INN/doubleclick-for-wp/issues/29).
4143
- Fixes a number of PHP warnings and errors, including [issue #8](https://github.com/INN/doubleclick-for-wp/issues/8) and [issue #37](https://github.com/INN/doubleclick-for-wp/issues/37) in [PR #76](https://github.com/INN/doubleclick-for-wp/pull/76) and [issue #31](https://github.com/INN/doubleclick-for-wp/issues/31) in [PR #80](https://github.com/INN/doubleclick-for-wp/pull/80/).
44+
- Fixes a targeting problem that occurred when wp_footer ran outside The Loop. ([PR #87](https://github.com/INN/doubleclick-for-wp/pull/87) for [this support topic](https://wordpress.org/support/topic/categories-tags-bug-and-solution/).)
4245
- Adds "Ad unit" label to widget settings for the "Identifier" setting, to match Google's language. [PR #73](https://github.com/INN/doubleclick-for-wp/pull/73) for [issue #26](https://github.com/INN/doubleclick-for-wp/issues/26).
4346
- Adds GitHub Pull Request template and Contributing guidelines files.
4447
- Adds a plugin text domain: `dfw`. ([PR #76](https://github.com/INN/doubleclick-for-wp/pull/76))

0 commit comments

Comments
 (0)