好啊此代码:
$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]);