简体   繁体   English

在 for 循环中过滤 PHP 个帖子

[英]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...下面的代码显示一个网格,其中包含“艺术家”的帖子缩略图,这些艺术家的帖子有“slug”、“描述”、“名称”等类别,所以我的网站显示每个人,但首先按谁的类别排序有更多的帖子,但我想做的是按照我用字母设置的“描述”来排序: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我试过切换 $filter_ = f-.{'slug'} 变量,甚至尝试解决计数问题,但没有任何进展,如果有人能指出正确的方向来修复这个 function 我非常感谢 <3

thank u感谢你

You should sort the $categories by it's description.您应该按描述对$categories进行排序。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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