Forums

[resolved] Category link hierarchy (2 posts)

  1. FamousFish
    Member
    Posted 7 months ago #

    On my main index page, each post has a category link.

    My Categories are locations in Canada with provinces being the parents and cities being the children.

    Is there a way to make the category ALWAYS display like "$child, $parent" every time?

    Currently the order changes based on alphabetizing, for instance "Alberta, Lethbridge" ($parent, $child) and "Brandon, Manitoba" ($child, $parent)

    thanks

  2. FamousFish
    Member
    Posted 7 months ago #

    OK I managed to figure it out on my own.

    First, in loop.php, the function get_the_category_list(); has to have the $parents parameter set to "multiple". (e.g. get_the_category_list(', ', multiple);

    Then in wp-includes/category-template.php I modified these lines in the function get_the_category_list();

    From:

    else {
    $i = 0;
    foreach ( $categories as $category ) {
    if ( 0 < $i )
    $thelist .= $separator;
    switch ( strtolower( $parents ) ) {
    case 'multiple':
    if ( $category->parent )
    $thelist .= get_category_parents( $category->parent, true, $separator );
    $thelist .= 'term_id ) . '" title="' . esc_attr( sprintf( __( "View all posts in %s" ), $category->name ) ) . '" ' . $rel . '>' . $category->name.'';
    break;
    case 'single':
    $thelist .= 'term_id ) . '" title="' . esc_attr( sprintf( __( "View all posts in %s" ), $category->name ) ) . '" ' . $rel . '>';
    if ( $category->parent )
    $thelist .= get_category_parents( $category->parent, false, $separator );
    $thelist .= "$category->name
    ";
    break;
    case '':
    default:
    $thelist .= 'term_id ) . '" title="' . esc_attr( sprintf( __( "View all posts in %s" ), $category->name ) ) . '" ' . $rel . '>' . $category->name.'';
    }
    ++$i;
    }
    }

    To:

    else {
    $i = 0;
    foreach ( $categories as $category ) {
    if ( 0 < $i )
    $thelist .= $separator;
    switch ( strtolower( $parents ) ) {
    case 'multiple':
    if ( $category->parent )
    //changed by me
    $thelist .= 'term_id ) . '" title="' . esc_attr( sprintf( __( "View all posts in %s" ), $category->name ) ) . '" ' . $rel . '>' . $category->name.''.$separator;
    $thelist .= get_category_parents( $category->parent, true,'');
    //end change
    break;
    case 'single':
    $thelist .= 'term_id ) . '" title="' . esc_attr( sprintf( __( "View all posts in %s" ), $category->name ) ) . '" ' . $rel . '>';
    if ( $category->parent )
    $thelist .= get_category_parents( $category->parent, false, $separator );
    $thelist .= "$category->name
    ";
    break;
    case '':
    default:
    $thelist .= 'term_id ) . '" title="' . esc_attr( sprintf( __( "View all posts in %s" ), $category->name ) ) . '" ' . $rel . '>' . $category->name.'';
    }
    ++$i;
    }
    }

Reply

You must log in to post.

About this Topic