Zweispaltige Artikelausgabe nach Kategorie mit WP_Query und sticky_posts

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

Artikelbild mit Zufalls-Bild als Fallback

  • start.php mit benutzerdefinierter Startseite auf der eine eigene Version des Artikelbilds (homepage-thumbnail) ausgegeben werden soll
  • single.php mit entsprechendem Artikelbild
  • ein Ordner /images/artikelbild/start/ im Theme-Ordner, in dem Bilder 1.jpg, 2.jpg und 3.jpg in den passenden Dimensionen des homepage-thumbnail abgelegt sind
  • ein Ordner /images/artikelbild/single/ im Theme-Ordner, in dem Bilder 1.jpg, 2.jpg und 3.jpg in den passenden Dimensionen des post-thumbnail abgelegt sind
  • hat ein Artikel kein Artikelbild, wird automatisch mittels Zufallsfunktion eines der Bilder aus den Ordnern angezeigt

functions.php

add_theme_support( 'post-thumbnails' );
set_post_thumbnail_size( 600, 200, true );
add_image_size( 'homepage-thumbnail', 290, 120, true );

start.php

<a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>" >
<?php if ( has_post_thumbnail() )
{ the_post_thumbnail( 'homepage-thumbnail', array('class' => 'homepage-thumbnail') ); }
else { ?>
<img src="<?php bloginfo('template_directory'); ?>/images/artikelbild/start/<?php echo mt_rand(1, 3); ?>.jpg" alt="<?php the_title(); ?>" class="homepage-thumbnail" />
<?php } ?></a>

single.php

<?php if ( has_post_thumbnail() )
{ the_post_thumbnail( 'post-thumbnail', array('class' => 'single-thumbnail') ); }
else { ?>
<img src="<?php bloginfo('template_directory'); ?>/images/artikelbild/single/<?php echo mt_rand(1, 5); ?>.jpg" alt="<?php the_title(); ?>" class="single-thumbnail" /><?php } ?>

Anpassen