WordPress.org

Support

Support » How-To and Troubleshooting » [Resolved] A 'post' with 4 cat…, when displayed appear sorted by name. How to sort by ID?

[Resolved] A 'post' with 4 cat…, when displayed appear sorted by name. How to sort by ID?

  • 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 . ' ';} ?>
Viewing 8 replies - 1 through 8 (of 8 total)
  • esmi
    Forum Moderator

    @esmi

    The categories will be sorted by ID by default.

    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.

    esmi
    Forum Moderator

    @esmi

    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 . ' ';
    }
    ?>

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

    esmi
    Forum Moderator

    @esmi

    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.

    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.

    esmi
    Forum Moderator

    @esmi

    Glad I could help 🙂

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘[Resolved] A 'post' with 4 cat…, when displayed appear sorted by name. How to sort by ID?’ is closed to new replies.