• Resolved akis

    (@akis)


    How can I redirect category to first available subcategory (child)?
    Is that possible?

    Thanks.

Viewing 4 replies - 1 through 4 (of 4 total)
  • Ideas of code and a plugin:

    https://www.ads-software.com/support/topic/257560?replies=6
    (note: you’d need to use something like get_categories to get the ‘first’ child)

    Thread Starter akis

    (@akis)

    Thanks.
    Now I have the following code:

    $thiscategory = get_query_var('cat');
    
    $catkids = get_categories("child_of=".$thiscategory."&sort_column=menu_order");
    
    if ($catkids) {
    $firstchild = $catkids[0];
    wp_redirect(get_permalink($firstchild->ID));
    }

    The problem is $firstchild->ID I think, get_permalink should be replaced with something like get_category_link.

    Problem solved, $firstchild->ID should be replace with $firstchild->term_id and of course get_category_link.

    This worked for me… includes @akis’ suggested corrections plus the proper parameters for get_categories (original snippet above uses parameters for “get_pages” per @michaelh’s original link).

    <?php // redirect parent category to first child
    $thiscategory = get_query_var('cat');
    
    $cat_query = array (
    	'child_of' => $thiscategory,
    	'orderby' => 'id',
    	'order' => 'desc'
    );
    
    $catkids = get_categories($cat_query);
    
    if ($catkids) {
    $firstchild = $catkids[0];
    wp_redirect(get_category_link($firstchild->term_id));
    }
    
    ?>

    For my purposes, I included this snippet in my custom category.php template.

    ack… actually, scratch that ^^ — causes an yet unresolved error: https://www.ads-software.com/support/topic/407556

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘How to redirect category to first available subcategory (child)’ is closed to new replies.