1

これは私の WordPress ループです。私はいくつかのことを試しましたが、思い通りに機能するものはありません。

while ( $loop->have_posts() ) : $loop->the_post();

$timestamp = get_post_meta( $post->ID, '_tatort_datum', true );

$ts_jahr = strftime("%Y",$timestamp);   
if($ts_jahr != $jahr_l) {
    echo '<h2 class="spacer_jahr" id="'. $ts_jahr .'">'. $ts_jahr .'</h2>';
} 
$jahr_l = $ts_jahr;


$ts_monat = strftime("%m",$timestamp);      
if($ts_monat != $monat_l) {
    echo '<h3 class="spacer_monat" id="'. strtolower($monat[$ts_monat]) . "_" . $ts_jahr. '">'. $monat[$ts_monat] .'</h3>';
} 
$monat_l = $ts_monat;

<article>here the content</article>

<?php endwhile; wp_reset_query(); ?>

出力は次のとおりです。

<h2>Headline</h2>
<h3>Headline</h3>
<article>Content</article>
<article>Content</article>
<article>Content</article>
<h3>Headline</h3>
<article>Content</article>
<article>Content</article>

しかし、私はこの出力が必要です:

<h2>Headline</h2>
<div class="month"> /* Here is the Change */
    <h3>Headline</h3>
    <article>Content</article>
    <article>Content</article>
    <article>Content</article>
<div>
<div class="month">
<h3>Headline</h3>
<article>Content</article>
</div>

しかし、私は今、これを行う方法がわかりません。div毎月新しい記事のコンテナーが必要なので、正しい方法を説明するヒントまたはコード スニペットが必要です。

4

1 に答える 1

1

ブール変数を使用して、が開い ている$divOpenかどうかを追跡できます...<div>

$divOpen = false;

while ($loop->have_posts()) : $loop->the_post();

    $timestamp = get_post_meta($post->ID, '_tatort_datum', true);

    $ts_jahr = strftime("%Y", $timestamp);
    if ($ts_jahr != $jahr_l) {
        if ($divOpen) {
            echo '</div>';
            $divOpen = false;
        }
        echo '<h2 class="spacer_jahr" id="'. $ts_jahr .'">'. $ts_jahr .'</h2>';
    }
    $jahr_l = $ts_jahr;

    $ts_monat = strftime("%m",$timestamp);
    if ($ts_monat != $monat_l) {
        if ($divOpen)
            echo '</div>';
        echo '<div class="month">';
        $divOpen = true;
        echo '<h3 class="spacer_monat" id="'. strtolower($monat[$ts_monat]) . "_" . $ts_jahr. '">'. $monat[$ts_monat] .'</h3>';
    } 
    $monat_l = $ts_monat;

    <article>here the content</article>

<?php endwhile; wp_reset_query(); ?>

if ($divOpen)
    echo '</div>';
于 2013-05-24T21:12:50.170 に答える