嘿,我正在尝试按最新发布日期排序我的类别。这意味着当我要添加帖子并将其标记在该类别下时,我希望该类别从最新到旧显示在顶部。另外,如果您有隐藏特定类别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>';
}
?>
答案 0 :(得分:0)
首先获取最新帖子,然后在获取类别运行后通过categori进行循环。
$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();