gpt4 book ai didi

php - 从多个配置文件按日期排序新闻

转载 作者:可可西里 更新时间:2023-10-31 22:44:58 25 4
gpt4 key购买 nike

我有几个配置文件,允许用户发布我想在一个长提要中显示的新闻,按日期顺序排序。

目前这会循环遍历每个个人资料,列出该个人资料中的新闻,然后移动到下一个个人资料,列出他们的新闻等等。它不会混淆所有内容。

如何将每个个人资料中的所有新闻混合在一起并按日期排序?

<?php global $post;
$args = array('numberposts' => -1);
$myposts = get_posts( $args );
foreach( $myposts as $post ) : setup_postdata($post); ?>
<?php while(the_repeater_field('news')): ?>
<div class="social-news-item">
<p class="social-news"><?php the_sub_field('news_description'); ?></p>
<p class="social-company"><?php echo the_title(); ?></p>
</div>
<?php endwhile; ?>
<? endforeach; wp_reset_postdata(); ?>

日期字段是:

<?php the_sub_field('date'); ?>

最佳答案

如果您想以编程方式更改顺序,请查看各种 array sorting functions in PHP , 特别是

  • uasort()— 使用用户定义的比较函数对数组进行排序并维护索引关联
  • uksort()— 使用用户定义的比较函数按键对数组进行排序
  • usort()— 使用用户定义的比较函数按值对数组进行排序

但是对于 wordpress 结帐这个 http://codex.wordpress.org/Template_Tags/get_posts

给定链接中的示例:

默认:

<?php 
$args = array(
'posts_per_page' => 5,
'numberposts' => 5,
'offset' => 0,
'category' => '',
'orderby' => 'post_date',
'order' => 'DESC',
'include' => '',
'exclude' => '',
'meta_key' => '',
'meta_value' => '',
'post_type' => 'post',
'post_mime_type' => '',
'post_parent' => '',
'post_status' => 'publish',
'suppress_filters' => true );
?>

使用:

 <?php $posts_array = get_posts( $args ); ?> 

希望这对你有帮助。

关于php - 从多个配置文件按日期排序新闻,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15498349/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com