【问题标题】:Order foreach $term by the date its added按添加日期订购 foreach $term
【发布时间】:2015-11-20 14:26:21
【问题描述】:

我正在使用$terms 来显示我创建的名为position 的自定义分类法的$term->name

但是,这些术语目前按字母顺序显示,我需要它们按照它们在管理面板中被选择/添加的顺序显示。

到目前为止,这是我的代码,我已经进行了简化,以便更容易看到我正在查看的区域更改顺序。

<?php
$posts = get_field('team_members',12);
if( $posts ): ?>
<?php foreach( $posts as $post): // variable must be called $post (IMPORTANT)
setup_postdata($post);
$terms = get_the_terms( $post->ID , 'position', array( 'orderby' => 'post_date', 'order' => 'DESC' ) ); ?>
    <article>   
        <?php
            $i = 1;
            foreach($terms as $term) {

            if($i > 1){ 
             echo ' / '.$term->name;
            } else { 
                echo $term->name;
            }   
            $i++;  
            } ?>        
    </article>
<?php endforeach; ?>
<?php wp_reset_postdata();?>
<?php endif; ?>

每个帖子可以有多个术语。如果有多个术语,则添加 / 以分隔术语。正是在这里,我需要按照选择/添加的方式对它们进行排序。

【问题讨论】:

  • get_the_terms() 不接受参数作为第三个参数(它没有第三个参数)。

标签: php wordpress foreach


【解决方案1】:

也许在创建数组后对数据进行排序?

function sortFunction( $a, $b ) {
    return strtotime($a["post_date"]) - strtotime($b["post_date"]);
}
usort($terms, "sortFunction");

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-09-07
    • 2021-11-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-15
    • 2021-04-14
    相关资源
    最近更新 更多