• smijos

    (@smijos)


    All I want is the parent category id for use with children. Here’s what Kafkaesqui wrote a few months back

    <?php
    $cat_object = $wp_query->get_queried_object();
    $cat_parent = $cat_object->category_parent;
    ?>

    but then what do I do with that? I tried to echo &cat_parent but that did nothing – doesn’t seem to matter whether I’m in or out of the loop

    this didn’t work either

    $cat = get_the_category();
    if ($cat->cat_parent = 0) {
    $cat_parent = $cat->cat_ID;
    } else {
    $cat_parent = $cat->cat_parent;
    }
    echo $cat_parent;

Viewing 3 replies - 1 through 3 (of 3 total)
  • use this:

    <?php
    $cat_object = $wp_query->get_queried_object();
    $parentcat = ($cat_object->category_parent) ? $cat_object->category_parent : $cat;
    wp_list_cats("child_of=$parentcat");
    ?>

    it works perfect — on archive.php.

    any chance to get this working on single.php? i played around with this but had no luck.

    None of this will work on a single post page.

    For that you’d have to collect it* through get_the_category(). Outside The Loop would involve this:

    <?php
    global $post;
    $categories = get_the_category();
    $category = $categories[0];
    $parent = ($category->category_parent) ? $category->category_parent : $category->cat_ID;
    wp_list_cats("child_of=$parent");
    ?>

    To alternate between single and category (archive) queries, something like this should do it:

    <?php
    if( is_single() ) {
    	global $post;
    	$categories = get_the_category();
    	$category = $categories[0];
    } else {
    	$category = $wp_query->get_queried_object();
    }
    $parent = ($category->category_parent) ? $category->category_parent : $category->cat_ID;
    
    if( $parent )
    	wp_list_cats("child_of=$parent");
    ?>

    * This is really meant for blogs with posts residing in just one category.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Trying to translate Kafkaesqui’ is closed to new replies.