Skip to content

Instantly share code, notes, and snippets.

@paulgosnell
Created October 8, 2011 08:57
Show Gist options
  • Save paulgosnell/1272044 to your computer and use it in GitHub Desktop.
Save paulgosnell/1272044 to your computer and use it in GitHub Desktop.
commonly used snippets of WP code
<?php if (has_post_thumbnail( $post->ID ) ): ?>
<?php $image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'single-post-thumbnail' ); ?>
<div class="bgImg" style="background:url(<?php echo $image[0]; ?>) no-repeat 50% 50%;"></div>
<?php endif; ?>
<?php
$args = array(
'numberposts' => 10,
'order'=> 'ASC',
'orderby' => 'title'
);
$postslist = get_posts( $args ); foreach ( $postslist as $post ) : setup_postdata($post); ?>
<?php the_date(); ?>
<?php the_title(); ?>
<?php the_excerpt(); ?>
<?php endforeach; ?>
Display a list of 5 posts selected randomly by using the MySQL RAND() function for the orderby parameter value:
<?php
$args = array(
'numberposts' => 5,
'orderby' => 'rand'
);
$rand_posts = get_posts( $args ); foreach ( $rand_posts as $post ) : ?>
<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
<?php endforeach; ?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment