使类别在使用$cat=‘’时适当更改;

时间:2012-03-05 作者:Michael

下面的代码基于几个地方的几个论坛帖子。在某种程度上,它是有效的。我遇到的问题是,它只显示“活动”,无论我属于哪一类家长。我尝试用一些非常不成功的替代品(条件、get函数等)替换“活动”;什么都没用。

<?php $cat = \'Activities\'; /* This is the problem area.
   Trying to get $cat to equal the name of the selected parent categories; the rest of
   the code seems to be working, at least for the Activities category. */

$catID = get_cat_ID($cat);
echo \'<h2>\' . $cat . \'<h2>\';
$subcats = get_categories(\'child_of=\' . $catID);
foreach($subcats as $subcat)
{
    echo \'<h4>\' . $subcat->cat_name . \' </h4>\';
    echo \'<ul>\';
    $subcat_posts = get_posts(\'cat=\' . $subcat->cat_ID);
    foreach($subcat_posts as $subcat_post)
    {
        echo \'<li>\';
        $postID = $subcat_post->ID;
        echo \'<a href="\' . get_permalink($postID) . \'">\';
        echo get_the_title($postID);
        echo \'</a>\';
        echo \'</li>\';
    }
    echo \'</ul>\';
}?>

1 个回复
最合适的回答,由SO网友:svassr 整理而成

如果您处于类别上下文中,这应该可以使用,并且您可以使用正确的catID。

function getCurrentCatID(){
    global $wp_query;
    if(is_category()){
        $cat_ID = get_query_var(\'cat\');
    }
    return $cat_ID;
}
$catID = getCurrentCatID();

结束