WordPress: Custom Post Types zusammenfassen und Archiv bilden

Steffen Giers

Erfahrenes Mitglied
Hallo,

ich versuch mit Wordpress eine Jahresarchiv (gruppiert nach Monaten) zu erstellen. Das Problem was ich habe, ist die Tatsache das -soweit ich weiß- selbst definierte Inhaltstypen nicht wie den WordPress entsprechenden Archiv-Funktionen genutzt werden können.

Deshalb hole ich mir mit query_posts() die gewünschten Inhaltstypen und kann mit diesen auch Arbeiten nur wird jeder Inhaltstype für sich alleine behandelt.

Der Code unten Produziert folgenden Output. In klammern steht der nachträglich hinzugefügte Inhaltstyp.

Code:
    Jul
        Vancouver Island (images)
        Willkommen auf meiner Website  (articles)
        Testeintrag (articles)
    Jul
       Ottawa (images)
      Vancouver (images)
PHP:
<?php query_posts (array ('post_type' => array('images', 'articles')));?>

    <?php
        if (have_posts()) : while (have_posts()) : the_post();

        // save month to a variable
        $month = the_date('M', '', '', FALSE);

        // If not used before print it 
        if ($month != $m_check) {
            echo "<h2>" . $month . "</h2>";
        }

        // save month to check variable
        $m_check = $month;
    ?>

    <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a><br/>

    <?php endwhile; ?>
    <?php endif; ?>
<?php wp_reset_query();?>

Ich muss ganz ehrlich zugeben das ich nicht verstehe warum die Sortierung so erfolgt wie Sie zu sehen ist. Ich würde hier erwarten das die Einträge (die alle im Juli enstanden sind) auch nach dem Monat gruppiert ausgegeben werden.

Viele Dank
 
Hier die Lösung.

PHP:
<?php

    $args = array(
        'post_type' => array('images', 'articles'),
        'numberposts' => -1,
        'post_status' => null,
        'post_parent' => null,
        );

    $posts = get_posts($args);

    if ($posts) {
        foreach ($posts as $post) {
            setup_postdata($post);
            $month =  mysql2date('m', $post->post_date);

            if ($month != $check) {
                echo "<h2>" . $month . "</h2>";
            }

            // save month to check variable
            $check = $month;

            echo $post->post_title;
            echo '<br/>';
        }
    }

    ?>
 

Neue Beiträge

Zurück