与父类别ID相关的子类别ID切换大小写

时间:2015-05-21 作者:niziou

我想切换页脚中的广告标签,具体取决于当前显示的类别。下面的代码适用于自定义类别页面和交换机机箱中包含的类别页面。我的代码寻找ID,问题是我有很多子类别,它们是我主要类别的子类别,因此这些类别的ID不同于父ID。

在不为每个儿童类别编写案例的情况下,我如何解决这个问题?

<?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;

?>

1 个回复
SO网友:s_ha_dum

好啊此代码:

$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;
}
其实没有必要。过一会儿我会告诉你我的意思。但是,稍后,您可以设置$cat_id_gum 到当前帖子ID。。。

$cat_id_gum = get_the_ID();
。。。这没有任何意义,使前面的所有代码都毫无意义。你还做了一些对我来说毫无意义的事情。我将忽略这一点,尝试解决简化代码和处理子类别的基本问题。

首先,让我们将类别和代码关联起来:

// the key is the category
// the value is the gum code
$gum_codes = array(
  \'1\' => \'75803baa\',
  \'2\' => \'b737914c\'
);
现在你可以echo 脚本如下:

printf($script,$gum_codes[1]);
现在,您需要将类别转换为其最顶层的父类别:

$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
把这些放在一起:

$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]);

结束

相关推荐

在数组中查询产品和存储ID

我试图在WooCommerce中查询产品,并将其ID作为一个数组返回,该数组可用于有条件过滤。我使用ACF Pro向产品中添加自定义字段,然后使用条件代码。我和我的开发人员朋友谈过,他建议采用这种方法,将数组存储在$ex_id或$inc_id变量中;<?php query_posts( $args ); while ( have_posts() ) : the_post(); ?> <?php if( in_array( \'yes\', get_f