Wordpress代码显示侧栏中的单个类别列表,不包括当前月份

时间:2014-03-31 13:51:48

标签: wordpress archive

我找到并调整了一些代码,以便在侧边栏中显示wordpress的每月列表中的单个类别。我有一个问题,我不希望它显示当前月份。不知道如何调整它。

<?php
                global $wpdb, $wp_locale;
                $query = "select YEAR(post_date) AS <code>year</code>, MONTH(post_date) AS <code>month</code>, count(ID) as posts from $wpdb->posts,  $wpdb->term_taxonomy, $wpdb->term_relationships
                    WHERE $wpdb->posts.post_status = 'publish'
                    AND $wpdb->posts.post_type = 'post'
                    AND $wpdb->term_taxonomy.term_id = 9
                    AND $wpdb->posts.ID = $wpdb->term_relationships.object_id
                    AND $wpdb->term_taxonomy.term_taxonomy_id = $wpdb->term_relationships.term_taxonomy_id
                 GROUP BY YEAR(post_date), MONTH(post_date) ORDER BY post_date DESC";
                 $arcresults = $wpdb->get_results($query);
                foreach ($arcresults as $arcresult):
                $text = sprintf(__('%1$s %2$d'), $wp_locale->get_month($arcresult->month), $arcresult->year);?>
                <li><a href="<?php bloginfo('url') ?>/<?php echo $arcresult->year; ?>/<?php echo str_pad($arcresult->month, 2, '0', STR_PAD_LEFT); ?>/<?php echo "?cat=9";  ?>"><?php echo $text;  ?> </li>
                  <?php endforeach; ?>

1 个答案:

答案 0 :(得分:0)

阅读WP_QueryDate_Query个对象的文档。您可以将Date_Query传递给WP_Query,这将在给定正确参数时过滤掉最近一个月。

使用WP_Query对象(对开发人员而言)比构建复杂的SQL查询更有效。