Support » Fixing WordPress » get_the_category order

  • Resolved gillsans

    (@gillsans)


    Is there anyway to reorder which category displays first?

    Using the get_the_category?

    <?php
    $category = get_the_category();
    echo $category[0]->cat_name;
    ?>
Viewing 10 replies - 1 through 10 (of 10 total)
  • Anyone know how to edit the order that categories are displayed?

    Not that I have an answer, but rather a question.

    In what order do you want to display the categories for a given post?

    Good Questions.

    Right now they display alphabetically, it would be nice if there was a way to sort then by first category added when the post was created.

    The reason being is I’m calling the first category to display at the topic of the blog post but this may not display my first choice but the alphabetical first.

    Hope that helps. Clear as mud 🙂

    Painfully clear 😉 Might try something like this:

    <?php
    $args=array('orderby' => 'none');
    $terms = wp_get_post_terms( $post->ID , 'category', $args);
    foreach($terms as $term) {
      echo '<a href="' . esc_attr(get_term_link($term, 'category')) . '" title="' . sprintf( __( "View all posts in %s" ), $term->name ) . '" ' . '>' . $term->name.'</a> ';
    }
    ?>

    That displays all of the categories. I only want to display the first category selected when creating the post.

    Then put a counter in that foreach that only iterates once.

    will do. thanks 🙂

    Hello Michael,

    Your solution works great. Do you think there is a chance to display only the last category => grandchild with your solution ?

    Marc Serra

    (@elseudomininet)

    Hello to all, 😉

    Hello Michael,

    Your solution works great. Do you think there is a chance to display only the last category => grandchild with your solution ?

    I’m interested too… anyone have a solution?

    hey Michael,

    is it possible to seperate the categories with a comma or any symbol in your solution?

Viewing 10 replies - 1 through 10 (of 10 total)
  • The topic ‘get_the_category order’ is closed to new replies.