简体   繁体   中英

Filtering PHP posts on a for loop

i'm working on a website that had installed a theme, im changing the functions but theres one that i cant fix.

The following code displays a grid with thumbnails of posts which are "artists", these artists posts have categories like "slug", "description", "name", so my website is displaying every person, but sorts it first by the category who has more posts, but what i want to do is order it by the "description" which i've set with letters like: A,B,C,D...

<?php 
    $fimg = get_the_post_thumbnail_url(get_the_ID(),'full'); //imagen
    $categories = wp_get_post_terms( $post->ID, 'filter'); //
    $c = $categories[0];
    $filtros = count($categories);
    $filter = '';
    for ($i=0; $i < $filtros; $i++) { 
        $f = $categories[$i];
        $filter_ = $f->{'slug'};
        $filter__ = $f->{'name'};
        if($i > 0){
        $text_f = $text_f.' - '.$filter__;
        } else {
        $text_f = $filter__;
        }
        $filter = $filter.' '.$filter_;
    };
    $url = get_permalink();
    $hover = rwmb_meta( 'talento-image_hover', array( 'size' => 'full' ) );
foreach ( $hover as $image ) {
     $hover = $image['url'];
}
?>

<div data-order="<?php echo $c->{'description'} ?>" class="col-lg-4 col-md-4 col-sm-12 talent grid-item my-3  <?php echo $filter; ?> thumb" id="post-<?php the_ID(); ?>">
    
    <?php echo get_the_post_thumbnail( $post->ID, 'large' ); ?>
    <div class="fdw-background" >
        <a href="<?= $url;?>">
        <img class="foto-artista" src="<?php echo $hover; ?>">
        </a>    
        <span class="talent-data">
<?php the_title( sprintf( '<h2 class="entry-title text-center"><a href="%s" rel="bookmark">', esc_url( get_permalink() ) ),
        '</a></h2>' ); ?>
        <p class="text-center col-lg-12"><?php echo $text_f; ?></p>
        </span>
    </div>
        <?php
        wp_link_pages( array(
            'before' => '<div class="page-links">' . __( 'Pages:', 'understrap' ),
            'after'  => '</div>',
        ) );
        ?>

    </div><!-- .entry-content -->

I've tried switching the $filter_ = f-.{'slug'} variables, even trying to do a work around with the count, but didnt got anywhere, if anyone can point me in the right direction to fix this function i'd be extremely grateful <3

thank u

You should sort the $categories by it's description.

usort($categories, function($a, $b) {
   return strcmp($a->description, $b->description);
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM