WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] A 'post' with 4 cat..., when displayed appear sorted by name. How to sort by ID? (9 posts)

  1. webipsum
    Member
    Posted 2 years ago #

    A 'post' with four categories. The categories when displayed, appear sorted by name.
    How to sort by ID?

    <?php foreach((get_the_category()) as $category) {
    echo $category->cat_name . ' ';} ?>
  2. esmi
    Forum Moderator
    Posted 2 years ago #

    The categories will be sorted by ID by default.

  3. webipsum
    Member
    Posted 2 years ago #

    The question is ...
    Example:
    "Post Number 1"
    It belongs to categories:
    Monday (ID 11)
    Tuesday (ID 12)
    Wednesday (ID 13)
    Thursday (ID 14)
    Friday (ID 15)
    Saturday (ID 16)
    Sunday (ID 17)
    When the loop displays the "Post Number 1", example:

    <?php the_title(); ?>
    <?php the_content(); ?>
    <?php the_category(' '); ?>
    or
    <?php foreach((get_the_category()) as $category) {
    echo $category->cat_name . ' ';} ?>

    The categories are shown in the following order (alphabetically):
    Friday Monday Saturday Sunday Thursday Tuesday Wednesday

    And not by ID.
    Monday Tuesday Wednesday Thursday Friday Saturday Sunday

    Tks.

  4. esmi
    Forum Moderator
    Posted 2 years ago #

    Sorry. Try something like:

    <?php
    $cat_names = array();
    foreach((get_the_category()) as $category) {
    	$cat_names[] = $category->cat_name;
    }
    sort( $cat_names );
    foreach( $cat_names as $cat_name } {
    	echo $cat_name . ' ';
    }
    ?>
  5. webipsum
    Member
    Posted 2 years ago #

    Thanks, esmi.
    This is almost what I need ...
    I need to display '$ category-> cat_name' but sorted by '$ category-> cat_id'.

  6. esmi
    Forum Moderator
    Posted 2 years ago #

    Try:

    <?php
    $cat_names = array();
    foreach((get_the_category()) as $category) {
    	$cat_names[$category_cat_id] = $category->cat_name;
    }
    ksort( $cat_names );
    foreach( $cat_names as $cat_name } {
    	echo $cat_name . ' ';
    }
    ?>

    Warning: not tested.

  7. webipsum
    Member
    Posted 2 years ago #

    Again, thank you, esmi.

    <?php
    $cat_names = array();
    foreach((get_the_category()) as $category) {
    	$cat_names[$category_cat_id] = $category->cat_name;
    }
    ksort( $cat_names );
    foreach( $cat_names as $cat_name ) {
    	echo $cat_name . ' ';
    }
    ?>

    It did not work ...

    I adapted:

    <?php
    $cat_names = array();
    foreach((get_the_category()) as $category) {
    	$cat_names[] = $category->cat_name;
    }
    sort( $cat_names );
    foreach( $cat_names as $cat_name ) {
    	echo $cat_name . ' ';
    }
    ?>

    To:

    <?php
    $cat_names = array();
    foreach((get_the_category()) as $category) {
    $cat_names[] = $category->cat_ID.$category->cat_name;
    }
    sort( $cat_names );
    foreach( $cat_names as $cat_name ) {
    echo substr($cat_name,3) . '  ';
    }
    ?>

    It worked perfectly.

    Thank you very much.

  8. esmi
    Forum Moderator
    Posted 2 years ago #

    Glad I could help :-)

  9. webipsum
    Member
    Posted 2 years ago #

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.