4.我正试着按本税期一个月的时间来报税

时间:2019-11-25 作者:Raminer

Hi please help me how can i display custom post count by month like this at current taxonomy page.

**我已经试着解决这个问题两周了。**

12月0日

1月0日

2月0日

3月0日至4月0日

5月0日

6月2日

7月8日

8月35日

9月0日

10月0日

11月9日

我做了我能做的一切。但结果为0或每月的总帖子数

<?php
global $wpdb;
$rel = $wpdb->get_results(
    "SELECT MONTH(post_date) as post_month, COUNT(ID) as post_count " .
    "FROM {$wpdb->posts} AS posts
                LEFT JOIN {$wpdb->term_relationships} AS tax_rel ON (posts.ID = tax_rel.object_id)
                LEFT JOIN {$wpdb->term_taxonomy} AS term_tax ON (tax_rel.term_taxonomy_id = term_tax.term_taxonomy_id)
                LEFT JOIN {$wpdb->terms} AS terms ON (terms.term_id = term_tax.term_id)" .
    "WHERE post_date BETWEEN DATE_SUB(NOW(), INTERVAL 12 MONTH) AND NOW() AND post_type = \'tesekkur\' " .
    "AND post_status = \'publish\' " . $post_type_query .
       "AND terms.name = \'term_id\'
        AND term_tax.taxonomy = \'term_taxonomy\'",
    "GROUP BY post_month ORDER BY post_date DESC", OBJECT_K
);

$postCount= 0;
$len = count($looper);

$cur = absint(date(\'n\'));
if($cur > 1)
{
    $looper = array_merge(range($cur+1, 12), range(1, $cur));
}
else
{
    $looper = range(1, 12);
}

$out = \'0,\';
$postCount= \'0\';
$len = count($looper);
foreach($looper as $m)
{


    $month = date_i18n(\'F\', mktime(0, 0, 0, $m, 1));

    $out .= sprintf(
       \'<li>\' .\'%s %d\'. \'</li>\',
        $month,
        //\'\',
        isset($rel[$m]) ? $rel[$m]->post_count : 0
    );
    if ($postCount!= $len-1) {
       $out .= \'\';
    }

    $postCount++;

}
//$out .= \'</ul>\';
echo $out; ?>

1 个回复
最合适的回答,由SO网友:Kaperto 整理而成

您可以使用以下WordPress函数来实现:

$post_type = "tesekkur";

$timestamp_start = strtotime("first day of next month -1 year midnight");
$start = date("Y-m-d H:i:s", $timestamp_start);

$posts = get_posts([
    "nopaging" => TRUE,
    "post_type" => $post_type,
    "date_query" => [
        "after" => $start,
    ],
]);


// sort by month

$tab = [];

foreach ($posts as $post) {

    $month = mysql2date("F", $post->post_date);

    if (!isset($tab[$month])) {
        $tab[$month] = 0;
    }

    $tab[$month]++;

}


// display

$timestamp = $timestamp_start;
$now = time();

while ($timestamp < $now) {

    $month = date_i18n("F", $timestamp);

    $count = $tab[$month] ?? 0; // need PHP 7

    echo "$month : $count<br/>";


    // next month

    $timestamp = strtotime("+1 month", $timestamp);

}

相关推荐

无法将ACF DatePicker值转换为DateTime对象-给我一个布尔值

我一直收到这个错误Call to a member function modify() on boolean 使用此代码$date_value = the_sub_field( \'date_string\' ); $date = DateTime::createFromFormat(\'d/m/Y\', $date_value); $date->modify(\'+1 day\'); 最后一行给了我一个错误。我的两种格式(在ACF中显示和返回)都相同\'d/m/Y\'真