WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] get_the_category order (11 posts)

  1. gillsans
    Member
    Posted 4 years ago #

    Is there anyway to reorder which category displays first?

    Using the get_the_category?

    <?php
    $category = get_the_category();
    echo $category[0]->cat_name;
    ?>
  2. gillsans
    Member
    Posted 4 years ago #

    Anyone know how to edit the order that categories are displayed?

  3. MichaelH
    Member
    Posted 4 years ago #

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

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

  4. gillsans
    Member
    Posted 4 years ago #

    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 :)

  5. MichaelH
    Member
    Posted 4 years ago #

    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> ';
    }
    ?>
  6. gillsans
    Member
    Posted 4 years ago #

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

  7. MichaelH
    Member
    Posted 4 years ago #

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

  8. gillsans
    Member
    Posted 4 years ago #

    will do. thanks :)

  9. jzk
    Member
    Posted 4 years ago #

    Hello Michael,

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

  10. Marc Serra
    Member
    Posted 3 years ago #

    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?

  11. ggurak
    Member
    Posted 3 years ago #

    hey Michael,

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

Topic Closed

This topic has been closed to new replies.

About this Topic