WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] How to redirect category to first available subcategory (child) (5 posts)

  1. akis
    Member
    Posted 4 years ago #

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

    Thanks.

  2. MichaelH
    Member
    Posted 4 years ago #

    Ideas of code and a plugin:

    http://wordpress.org/support/topic/257560?replies=6
    (note: you'd need to use something like get_categories to get the 'first' child)

  3. akis
    Member
    Posted 4 years ago #

    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.

  4. luispunchy
    Member
    Posted 4 years ago #

    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.

  5. luispunchy
    Member
    Posted 4 years ago #

    ack... actually, scratch that ^^ --- causes an yet unresolved error: http://wordpress.org/support/topic/407556

Topic Closed

This topic has been closed to new replies.

About this Topic