【问题标题】:Show category by latest post date按最新发布日期显示类别
【发布时间】:2016-10-19 08:34:30
【问题描述】:

我在一个项目中工作,我使用下面的代码来限制每个类别中的帖子数量。但我不知道如何按最新帖子对这些类别进行排序。我的代码工作正常,但是当我在 wp 查询数组 中添加参数时,它不会排序或中断。我想订购具有最近最近发布(日期)的类别。

谁能指导我?

<?php $terms=g et_terms( array( 'taxonomy'=>'category', 'hide_empty' => false, ) ); foreach($terms as $cat){ $cata_name = $cat->name; $term_id = $cat->term_id; ?>
<?php //echo '<h3>'.$catname[0]->cat_name.'</h3>'; ?>
<?php $catqueryy=n ew WP_Query ( 'cat='.$term_id. '&posts_per_page=5');$count=$ catqueryy->found_posts; ?>

【问题讨论】:

  • 您使用的是哪个 SQL DBMS?
  • 从数据库中选择数据时对数据进行排序。使用 order by 并使用 date 列。
  • 我正在使用 phpmyadmin 模块...这些是我的 wordpress 代码..
  • 你在哪里添加argument
  • @devpro <?php $terms = get_terms( array( 'taxonomy' => 'category', 'hide_empty' =&gt; false, ) ); foreach($terms as $cat){ $cata_name = $cat-&gt;name; $term_id = $cat-&gt;term_id; ?&gt;

标签: php sql wordpress taxonomy-terms


【解决方案1】:

您可以在 wordpress 中添加 ORDER BY 参数为:

<?php
$args = array(
    'post_type'  => 'post', //your_post_type
    'orderby'    => 'date', //meta_value
    'meta_query' => array(array('key' => 'cat','value'=>$term_id)),
    'order'      => 'DESC',
    'posts_per_page' => 5,
);

$catqueryy = new WP_Query($args);
?>

您可以浏览 wordpress 文档:https://codex.wordpress.org/Class_Reference/WP_Query#Order_.26_Orderby_Parameters

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-10-08
  • 1970-01-01
  • 2016-08-29
  • 2021-07-06
相关资源
最近更新 更多