skip to Main Content

i have created this Php code to show a list by category.

Actually the list shows by alphabetical order.

I would like to show by create date.

<?php
$terms = get_terms( 'category' );
if ( ! empty( $terms ) && ! is_wp_error( $terms ) ){
    echo '<div class="product-category-list">';
    $x = 1;
    foreach ( $terms as $term ) {
        echo "<div class='product-category-item bg-class-{$x}'>";
        $icon = pods_field( 'category', $term->term_id, 'immagine_categoria', true );
        $icon_url = $icon['guid'];
        $icon_thumb = pods_image_url ( $icon_url, 'large', 0, false ) ;
        $term_link = get_term_link($term);
     
        echo '<div class="product-category-image">';
        echo '<img src="'.$icon_thumb.'" alt="">';
        echo '</div>';
        echo '<div class="product-category-title">';
        echo '<h3>'.$term->name.'</h3>';
        echo '</div>';
        echo '<div class="product-category-description">'.$term->description.'</div>'; 
        echo '<div class="product-category-link">';
        echo '<a class="product-category-button" href="'.$term_link.'">Visualizza Categoria</a>';
        echo '</div>';
        echo '</div>';

        $x++;

        if ($x > 3) {
            $x = 1;
        }

    }
    echo '</div>';

}

Thank you.

2

Answers


  1. Lets try to sort the array first then display it. These are the some function that uses to sort the array.

    asort() //sort associative arrays in ascending order, according to the value
    ksort() //sort associative arrays in ascending order, according to the key
    arsort() //sort associative arrays in descending order, according to the value
    krsort() //sort associative arrays in descending order, according to the key
    

    Also check this article.
    https://www.codegrepper.com/code-examples/php/php+array+sort+by+column

    Login or Signup to reply.
  2. There is no orderby date in get_terms(). check below comments.

     *     @type string       $orderby                Field(s) to order terms by. Accepts:
     *                                                - term fields ('name', 'slug', 'term_group', 'term_id', 'id',
     *                                                  'description', 'parent', 'term_order'). Unless `$object_ids`
     *                                                  is not empty, 'term_order' is treated the same as 'term_id'.
     *                                                - 'count' to use the number of objects associated with the term.
     *                                                - 'include' to match the 'order' of the $include param.
     *                                                - 'slug__in' to match the 'order' of the $slug param.
     *                                                - 'meta_value', 'meta_value_num'.
     *                                                - the value of `$meta_key`.
     *                                                - the array keys of `$meta_query`.
     *                                                - 'none' to omit the ORDER BY clause.
     *                                                Defaults to 'name'.
     *     @type string       $order                  Whether to order terms in ascending or descending order.
     *                                                Accepts 'ASC' (ascending) or 'DESC' (descending).
    

    So you can pass 'orderby => 'ID', and 'order' => 'DESC' so this will give you category by created order. check below code.

    <?php
    $args = array(
        'taxonomy' => 'category',
        'orderby'  => 'id', 
        'order'    => 'DESC'
    ); 
    $terms = get_terms( $args );
    if ( ! empty( $terms ) && ! is_wp_error( $terms ) ){
        echo '<div class="product-category-list">';
        $x = 1;
        foreach ( $terms as $term ) {
            echo "<div class='product-category-item bg-class-{$x}'>";
            $icon = pods_field( 'category', $term->term_id, 'immagine_categoria', true );
            $icon_url = $icon['guid'];
            $icon_thumb = pods_image_url ( $icon_url, 'large', 0, false ) ;
            $term_link = get_term_link($term);
         
            echo '<div class="product-category-image">';
            echo '<img src="'.$icon_thumb.'" alt="">';
            echo '</div>';
            echo '<div class="product-category-title">';
            echo '<h3>'.$term->name.'</h3>';
            echo '</div>';
            echo '<div class="product-category-description">'.$term->description.'</div>'; 
            echo '<div class="product-category-link">';
            echo '<a class="product-category-button" href="'.$term_link.'">Visualizza Categoria</a>';
            echo '</div>';
            echo '</div>';
    
            $x++;
    
            if ($x > 3) {
                $x = 1;
            }
    
        }
        echo '</div>';
    
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search