0

I would like to switch ads tags in my footer depending on which category it displays at the moment. Code from below works fine for custom category pages and category pages that are included in the switch case. My code looking for ID, the problem is I have a lot of subcategories which are children of my main categories so the ID of those categories are different from parent IDs.

How do I address this issue without writing a case for each children categories?

<?php
 $footer_ads='';
 if(is_single())
    {
global $post;
$category = get_the_category($post->ID);
$cat_id_gum="";
if(($category[0]->parent)!='0')
{
    $cat_id_gum=$category[0]->cat_ID;
}
elseif(($category[1]->parent)!='0')
{
    $cat_id_gum=$category[1]->cat_ID;
}
elseif(($category[2]->parent)!='0')
{
    $cat_id_gum=$category[2]->cat_ID;
}
else
{
    $cat_id_gum=$category[3]->cat_ID;
}

}
elseif(is_page())
{

global $post;
$cat_id_gum = get_the_ID();
switch ($cat_id_gum)
{
    //GumGum tag - NFN/Sports
    case '216044': $footer_ads.='<script type="text/javascript">ggv2id=\'75803baa\';</script> <script type="text/javascript" src="http://g2.gumgum.com/javascripts/ggv2.js"></script>';break;
    //GumGum tag - NFN/Tech
    case '216042' : $footer_ads.='<script type="text/javascript">ggv2id=\'b737914c\';</script> <script type="text/javascript" src="http://g2.gumgum.com/javascripts/ggv2.js"></script>';break;
    //GumGum tag - NFN/Lifestyle
    case '216039' : $footer_ads.='<script type="text/javascript">ggv2id=\'8ba59d8a\';</script> <script type="text/javascript" src="http://g2.gumgum.com/javascripts/ggv2.js"></script>';break;
    //GumGum tag - NFN/Business
    case '216026' : $footer_ads.='<script type="text/javascript">ggv2id=\'5e3d4a16\';</script> <script type="text/javascript" src="http://g2.gumgum.com/javascripts/ggv2.js"></script>';break;
    //GumGum tag - NFN/Entertainment
    case '216036' : $footer_ads.='<script type="text/javascript">ggv2id=\'5c8180ad\';</script> <script type="text/javascript" src="http://g2.gumgum.com/javascripts/ggv2.js"></script>';break;
    default : $footer_ads.='<script type="text/javascript">ggv2id=\'c975bbec\';</script><script src="//g2.gumgum.com/javascripts/ggv2.js"></script>';break;

}
}
 elseif(is_category() ) {
global $post;
$category = get_category(get_query_var('cat'));
$cat_id_gum = $category->cat_ID;
switch ($cat_id_gum) {
    //GumGum tag - NFN/Sports
    case '57':
        $footer_ads .= '<script type="text/javascript">ggv2id=\'75803baa\';</script> <script type="text/javascript" src="http://g2.gumgum.com/javascripts/ggv2.js"></script>';
        break;
    //GumGum tag - NFN/Tech
    case '71' :
        $footer_ads .= '<script type="text/javascript">ggv2id=\'b737914c\';</script> <script type="text/javascript" src="http://g2.gumgum.com/javascripts/ggv2.js"></script>';
        break;
    //GumGum tag - NFN/Lifestyle
    case '76' :
        $footer_ads .= '<script type="text/javascript">ggv2id=\'8ba59d8a\';</script> <script type="text/javascript" src="http://g2.gumgum.com/javascripts/ggv2.js"></script>';
        break;
    //GumGum tag - NFN/Business
    case '19' :
        $footer_ads .= '<script type="text/javascript">ggv2id=\'5e3d4a16\';</script> <script type="text/javascript" src="http://g2.gumgum.com/javascripts/ggv2.js"></script>';
        break;
    //GumGum tag - NFN/Entertainment
    case '85' :
        $footer_ads .= '<script type="text/javascript">ggv2id=\'5c8180ad\';</script> <script type="text/javascript" src="http://g2.gumgum.com/javascripts/ggv2.js"></script>';
        break;
    default :
        $footer_ads .= '<script type="text/javascript">ggv2id=\'c975bbec\';</script><script src="//g2.gumgum.com/javascripts/ggv2.js"></script>';
        break;

}
}
 elseif(is_single() ) {
global $post;
$cat_id_gum = get_the_ID();
switch ($cat_id_gum) {
    //GumGum tag - NFN/Sports
    case '57':
        $footer_ads .= '<script type="text/javascript">ggv2id=\'75803baa\';</script> <script type="text/javascript" src="http://g2.gumgum.com/javascripts/ggv2.js"></script>';
        break;
    //GumGum tag - NFN/Tech
    case '71' :
        $footer_ads .= '<script type="text/javascript">ggv2id=\'b737914c\';</script> <script type="text/javascript" src="http://g2.gumgum.com/javascripts/ggv2.js"></script>';
        break;
    //GumGum tag - NFN/Lifestyle
    case '76' :
        $footer_ads .= '<script type="text/javascript">ggv2id=\'8ba59d8a\';</script> <script type="text/javascript" src="http://g2.gumgum.com/javascripts/ggv2.js"></script>';
        break;
    //GumGum tag - NFN/Business
    case '19' :
        $footer_ads .= '<script type="text/javascript">ggv2id=\'5e3d4a16\';</script> <script type="text/javascript" src="http://g2.gumgum.com/javascripts/ggv2.js"></script>';
        break;
    //GumGum tag - NFN/Entertainment
    case '85' :
        $footer_ads .= '<script type="text/javascript">ggv2id=\'5c8180ad\';</script> <script type="text/javascript" src="http://g2.gumgum.com/javascripts/ggv2.js"></script>';
        break;
    default :
        $footer_ads .= '<script type="text/javascript">ggv2id=\'c975bbec\';</script><script src="//g2.gumgum.com/javascripts/ggv2.js"></script>';
        break;

}
}
 echo $cat_id_gum;
 echo $footer_ads;

?>
  • I see a few ways to improve the code but I'd need to know the relationship between the category and this number -- 75803baa-- to answer the question. That looks like the only thing that changes from category to category. – s_ha_dum May 21 '15 at 19:09
  • ok, well I was given those tags from adverts company GumGum and this numbera which are changing, are related to 6 ads zones, so this one "75803baa" is suppose to be placed in the footer in Sport category and every subcategory which is inside of sport (same for all those posts which are inside sport/sub_sport_category.) I've commented names of the tags and the category they're belonging. – niziou May 21 '15 at 19:19
  • Are those all of them? All of the IDs? – s_ha_dum May 21 '15 at 19:23
  • There are 6 ADs IDs just like above, There are few more main categories and a many more subcategory (nearly 100) tha's why I'm looking a way to hook parents ID to children's so I won't need to worry about every single case – niziou May 21 '15 at 19:49
0

Ok. This code:

$cat_id_gum="";
if(($category[0]->parent)!='0')
{
    $cat_id_gum=$category[0]->cat_ID;
}
elseif(($category[1]->parent)!='0')
{
    $cat_id_gum=$category[1]->cat_ID;
}
elseif(($category[2]->parent)!='0')
{
    $cat_id_gum=$category[2]->cat_ID;
}
else
{
    $cat_id_gum=$category[3]->cat_ID;
}

Is not really necessary. I'll show you what I mean in a bit. Later, however, you then set $cat_id_gum to the current post ID...

$cat_id_gum = get_the_ID();

... which doesn't make any sense and makes all of the preceding code pointless. You do a couple of other things which don't really make sense to me. I am going to ignore that and try to address the basic question of simplifying the code and dealing with child categories.

First, lets associate the categories and the codes:

// the key is the category
// the value is the gum code
$gum_codes = array(
  '1' => '75803baa',
  '2' => 'b737914c'
);

Now you can echo a script like:

printf($script,$gum_codes[1]);

Now, you need to convert your category to its topmost parent:

$category = get_the_category($post->ID);
$cat_id_gum = $category[0]->cat_ID;
$cat_id_gum = get_ancestors($cat_id_gum,'category');
array_shift($cat_id_gum);
$cat_id_gum = $cat_id_gum[0];
// $cat_id_gum is now your parent category

Put that all together:

$category = get_the_category($post->ID);
$cat_id_gum = $category[0]->cat_ID;
$cat_id_gum = get_ancestors($cat_id_gum,'category');
array_shift($cat_id_gum);
$cat_id_gum = $cat_id_gum[0];
// $cat_id_gum is now your parent category

$script = '<script type="text/javascript">ggv2id=\'%s\';</script> <script type="text/javascript" src="http://g2.gumgum.com/javascripts/ggv2.js"></script>';

// the key is the category
// the value is the gum code
$gum_codes = array(
  '1' => '75803baa',
  '34' => 'b737914c'
);

printf($script,$gum_codes[$cat_id_gum]);
|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.