Zweispaltige Artikelausgabe nach Kategorie mit WP_Query und sticky_posts

Dieser Text wurde am 14. August 2013 veröffentlicht und enthält möglicherweise veraltete Informationen.

start.php (Benutzerdefiniertes Seitentemplate)

  • eine CSS-Klasse frontcoluml und eine frontcolumr, die die beiden Spalten formatiert
  • mit der PHP-Variable $postcount wird gezählt, wie viele Beiträge bereits ausgegeben wurden; mit $showposts wird dann die Zahl der noch auszugebenden weiteren Beiträge ermittelt
  • es wird in diesem Beispiel davon ausgegangen das pro Spalte drei Beiträge (post_per_page => 3) aus Kategorie fünf (cat => 5) ausgegeben werden sollen
<div class="frontcoluml">
<?php
$temp = $wp_query;
$wp_query = new WP_Query( array( 'posts_per_page' => '3', 'cat' => 5, 'post__in' => get_option('sticky_posts')));
while ($wp_query->have_posts()) : $wp_query->the_post();
?>
<h3><a href="<?php the_permalink() ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></h3>
<div class="entry">
<?php the_excerpt(); ?>
</div>
<?php
$postcount++;
endwhile;
wp_reset_postdata();
$showposts = 3 - $postcount;
if ($showposts != 0) {
$temp = $wp_query;
global $more; $more = 0;
$wp_query = new WP_Query( array( 'posts_per_page' => $showposts, 'ignore_sticky_posts' => 1, 'post__not_in' => get_option('sticky_posts'), 'cat' => 5));
while ($wp_query->have_posts()) : $wp_query->the_post();
?>
<h3><a href="<?php the_permalink() ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></h3>
<div class="entry">
<?php the_excerpt(); ?>
</div>
<?php endwhile;
wp_reset_postdata();
} else {}
unset($postcount);
unset($showposts);
?>
</div>

<div class="frontcolumr">
<?php
$temp = $wp_query;
$wp_query = new WP_Query( array( 'posts_per_page' => '3', 'cat' => 5, 'offset' => 3, 'post__in' => get_option('sticky_posts')));
while ($wp_query->have_posts()) : $wp_query->the_post();
?>
<h3><a href="<?php the_permalink() ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></h3>
<div class="entry">
<?php the_excerpt(); ?>
</div>
<?php
$postcount++;
endwhile;
wp_reset_postdata();
$showposts = 3 - $postcount;
if ($showposts != 0) {
$temp = $wp_query;
global $more; $more = 0;
$wp_query = new WP_Query( array( 'posts_per_page' => $showposts, 'ignore_sticky_posts' => 1, 'post__not_in' => get_option('sticky_posts'), 'cat' => 5, 'offset' => 3,));
while ($wp_query->have_posts()) : $wp_query->the_post();
?>
<h3><a href="<?php the_permalink() ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></h3>
<div class="entry">
<?php the_excerpt(); ?>
</div>
<?php endwhile;
wp_reset_postdata();
} else {}
unset($postcount);
unset($showposts);
?>
</div>

Referenz