Show terms of a taxonomy randomly

Asked

Viewed 57 times

0

I am displaying all categories with their respective images, according to the code below:

<?php
categorias = apply_filters('taxonomy-images-get-terms', '', array( 'taxonomy' => 'category') );
foreach($categorias as $key => $categoria):             
    if($key < 6):
    ?>
    <div class="box">
        <a href="<?php echo get_category_link($categoria->term_id);?>">
            <figure>
                <?php echo wp_get_attachment_image($categoria->image_id,''); ?>
            </figure>
            <img src="<?php bloginfo('template_directory');?>/images/icones/plus.png" class="plus-icone" alt="">    
            <figcaption class="titulo-foto-categoria"><?php echo $categoria->name;?></figcaption>
        </a>
    </div>
    <?php
    endif;
endforeach;?>

The function was used apply_filters to be able to display the image of the category, as far as I could find on the internet only that way it is possible to do this. From what I noticed this function shows all the results in alphabetical order and I need to show them randomly or by date.

It is possible to do this?

1 answer

1


Apparently apply_filters is always returning an array there, so just reorder the array using shuffle to get jumbled:

$categorias = apply_filters('taxonomy-images-get-terms', '', array( 'taxonomy' => 'category') );

shuffle( $categorias );

foreach ( $categorias as $key => $categoria ) : //etc
  • 1

    You talk about reordering by date, but you don’t have a date reference in the code. If you know which parameter brings this information I can put an example.

  • I have no way to test now, but getting shuffled will already solve my problem. Tomorrow early I will test. Thank you very much.

  • Ricardo, you did it right, I thank you again for your help. I had not even thought to search a way to reorder the array, I focused on the functions of Wordpress.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.