WordPress.org

Ready to get started?Download WordPress

Forums

Trying to translate Kafkaesqui (4 posts)

  1. smijos
    Member
    Posted 7 years ago #

    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;
  2. muskote
    Member
    Posted 7 years ago #

    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.

  3. sophiavancouver
    Member
    Posted 6 years ago #

  4. Kafkaesqui

    Posted 6 years ago #

    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.

Topic Closed

This topic has been closed to new replies.

About this Topic