0

日付の配列を生成しました。ここに簡単なサンプルがあります

Array
(
[2013] => Array
    (
        [Feb] => 2013
        [Jan] => 2013
    )

[2012] => Array
    (
        [Oct] => 2012
        [Jun] => 2012
        [May] => 2012
    )

)

サンプルコード:

<?php
$posts = get_posts(array('numberposts' => -1, 'post_status' => 'publish', 'post_type' => 'post', ));
#       $i=1;
print '<pre>';
foreach ($posts as $post) {
    #       $post_results[$post->ID][month] = get_the_time('m', $post->ID);
    #       $post_results[$post->ID][year]  = get_the_time('Y', $post->ID);
    $year = get_the_time('Y', $post -> ID);
    $month = get_the_time('M', $post -> ID);
    $post_results[$year][$month] = get_the_time('Y', $post -> ID);
    #       echo "$i. Post ID: "  .$post->ID." - " .get_the_time('Y-m-d', $post->ID)."<br/>";
    #       $i++;
}

foreach ($post_results as $key => $value) {
    echo "Month: " . $key . " Year: " . $value . "<br/>";
}

print_r($post_results);
print '</pre>';
?>

すべての日付をこの Web ページhttp://www.smartpassiveincome.com/archives/と同じ形式でリストしたいと思います。

私が必要としているのは、上記のように値を区切るために配列を取得することです。私はリンク部分を自分で行うことができます。データを使いやすい変数に取り込む必要があるだけです。

たとえば、「各年について、リストされているすべての月を取得し、入力して<td>$year</td><td>$month</td>から前の年に移動します。

4

3 に答える 3

1

これがあなたが必要だと思うものです。彼は次のことを達成する1つの方法です:

2013年 | 2月 | 2012 年1
月 | 10月 | ジュン | 5月

オリジナル:

$arrYears = array(2013=>array("Feb"=>2013, "Jan"=>2013), 2012=>array("Oct"=>2013, "Jun"=>2013, "May"=>2013));
        foreach($arrYears as $strYear=>$arrMonths) {
            echo $strYear;
            // This is our logic for each year
            foreach($arrMonths as $strMonth=>$strYear2) {
                echo " | ".$strMonth;
            }
            echo "<br />";
        }

編集:ネストされたリストに月を入れてください

 $arrYears = array(
                 2013=>array("Feb"=>2013, "Jan"=>2013),
                 2012=>array("Oct"=>2013, "Jun"=>2013, "May"=>2013)
              );

 echo "<ul>";
     foreach($arrYears as $strYear=>$arrMonths) {
         echo "<li>";
             echo $strYear;
             echo "<ul>";

                foreach($arrMonths as $strMonth=>$strYear2) {
                    echo "<li><a href='#'>".$strMonth."</a></li>";
                }

             echo "</ul>";
         echo "</li>";
     }
 echo "</ul>";
于 2013-02-13T15:12:55.243 に答える
1

これにより、必要なものが得られます。$currentDate開始したい日付のタイムスタンプに変更するだけです。

$currentDate = strtotime('January 1st, 2000');
$newestDate = strtotime(date('Y-m-01'));
$yearMonthArray = array();

while ($currentDate <= $newestDate) {
    if (!array_key_exists($year = date('Y', $currentDate), $yearMonthArray)) {
        $yearMonthArray[$year] = array();
    }

    $yearMonthArray[$year][] = date('M', $currentDate);

    $currentDate = strtotime('next month', $currentDate);
}

var_dump($yearMonthArray);

編集:これにより、すべての年/月の組み合わせが得られます。この例をコードと組み合わせて使用​​すると、投稿のある月のみを表示するものを取得できるはずです。

于 2013-02-13T14:57:33.150 に答える
0

私は自分でそれを理解しました、これが私の新しいコードです

$posts = get_posts(
    array(
        'numberposts' => -1,
        'post_status' => 'publish',
        'post_type' => 'post',
    )
);
foreach ($posts as $post) {
    $year = get_the_time('Y', $post->ID);
    $month = get_the_time('M', $post->ID);
    $post_results[$year][$month]  = $month;
}

foreach ($post_results as $key => $value) { 
    echo "<b>"  .$key."</b> ";

    foreach ($value as $sub_key => $sub_value) { 
        echo $sub_key." ";
    }
    echo "<br/>";

}
于 2013-02-13T15:15:38.480 に答える