php - 如何按最近的发布日期对 Wordpress 博客类别进行排序?
问题描述
大家好,我正在尝试按最近的发布日期排序我的类别。这意味着当我添加帖子并将其标记在该类别下时,我希望该类别从最新到旧显示在顶部。此外,如果您有办法隐藏某个类别 ID,我很想知道如何。我是 PHP 新手,希望能得到一些帮助,谢谢大家。
代码:
<?php
$cat_args = array(
'orderby' => 'date',
'post_type' => 'products',
'order' => 'ASC',
'child_of' => 0,
);
$categories = get_categories($cat_args);
foreach($categories as $category) {
echo '<dl>';
echo '<h3 class="category-name">' . $category->name.'</h3>';
$post_args = array(
'numberposts' => -1,
'category' => $category->term_id
);
$posts = get_posts($post_args);
foreach($posts as $post) {
?>
<dd><a class="article" target="_blank" href="<?php the_field('article_link') ?>"><?php the_title(); ?></a><span class="news-source"> - <?php the_field('news_source') ?></span><p class="important"><?php the_field('important') ?></p></dd>
<?php
}
//echo '<dd class="view-all"> <a href="' . get_category_link( $category->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . '>View all posts in ' . $category->name.'</a></dd>';
echo '</dl>';
}
?>
解决方案
首先获取最近的帖子,然后在获取类别后通过类别运行循环从中获取类别。
$args = array( 'numberposts' => '-1' );
$recent_posts = wp_get_recent_posts( $args );
foreach( $recent_posts as $recent ){
echo '<dl>';
echo '<h3 class="category-name">' . get_the_category_list( ', ', '', $recent["ID"] ).'</h3>';
$post_args = array(
'numberposts' => -1,
'category' => $recent["ID"]
);
$posts = get_posts($post_args);
foreach($posts as $post) {
?>
<dd><a class="article" target="_blank" href="<?php the_field('article_link') ?>"><?php the_title(); ?></a><span class="news-source"> - <?php the_field('news_source') ?></span><p class="important"><?php the_field('important') ?></p></dd>
<?php
}
//echo '<dd class="view-all"> <a href="' . get_category_link( $category->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . '>View all posts in ' . $category->name.'</a></dd>';
echo '</dl>';
}
wp_reset_query();
推荐阅读
- python - 通过串口获取的 int 值输出错误
- php - 将数组副本传递给期望引用的函数
- azure-devops - 无权通过运行管道的 Azure DevOps REST API 更新变量组
- c# - 如何扩展相同的应用程序但使用不同的配置
- gitlab - GitLab-runner 测试执行
- laravel-5 - 页面刷新后 Laravel 注销另一个 Laravel 项目
- pinterest - 为 Kafa 主题设置 Secor 的步骤
- javascript - PhoneGap - 安装 cordova-plugin-wkwebview-engine-localhost 插件时遇到问题
- regex - PHP Mysql在where子句中精确多次搜索
- laravel - created(){axios.get 方法在尝试显示数据库中的内容时不起作用