get_posts() – Beiträge inklusive Thumbnails ausgeben
Welcher Post darf es sein? Mit Hilfe der Funktion get_posts( $args )
können Posts sehr flexibel eingebunden werden. Diverse Argumente dienen zur entsprechenden Filterung aller verfügbaren Posts, sodass auch die Ausgabe sehr vielfältig gestaltet werden kann.
get_posts( $args ) – Ein kleiner Einblick
Der folgende Code stellt die Anwendung dar. Das Array $args
dient in gewisser Weise zur Filterung – Welche Inhalte (post
, attachment
, …) sollen wie (order
/ order_by
) ausgegeben werden. Sofern keine Angaben gemacht werden, sind die Standardwerte für die Ausgabe zuständig.
$args = array() $myPosts = get_posts( $args ) foreach( $myPosts as $post ) : [...] endforeach;
get_posts( $args ) – Beispiel
<?php $args = array ( 'post_type' => 'post', 'posts_per_page' => '12', 'ignore_sticky_posts' => true, 'orderby' => 'date', 'order' => 'desc' ); $myPosts = get_posts( $args ); $counter = 0; ?> <div class="clearfix"> <?php foreach( $myPosts as $post ): $postTitle = $post->post_title; $imgSrc = get_bloginfo( 'stylesheet_directory' ) . '/images/thumbnail-error404.png'; ?> <div class="post span3"> <div class="post-img"> <?php if(has_post_thumbnail()): echo the_post_thumbnail(array(232,232)); else: echo '<img src="'.$imgSrc.'" alt="" title="" />'; endif; ?> </div> <div class="post-content"> <?php echo '<a href="' . get_the_permalink() . '" title="">' .$postTitle. '</a>'; ?> </div> </div> <?php $counter++; endforeach; ?> </div>
Quellen:
http://codex.wordpress.org/Template_Tags/get_posts
Veröffentlicht am 21. September 2014