Skip to content

Instantly share code, notes, and snippets.

@cornelisonc
Last active April 6, 2018 21:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cornelisonc/500f166a85e138030a57d6d1dde8298b to your computer and use it in GitHub Desktop.
Save cornelisonc/500f166a85e138030a57d6d1dde8298b to your computer and use it in GitHub Desktop.
WordPress blog archive: featured image or dynamic random featured image
<div class="list-article-thumb">
<a href="<?php echo esc_url( get_permalink() ); ?>">
<?php
if ( has_post_thumbnail( ) ) {
the_post_thumbnail();
} else {
$images_iterator = new FilesystemIterator(
WP_CONTENT_DIR . '/themes/yourthemename/images/',
FilesystemIterator::SKIP_DOTS
);
$rand = rand(1, iterator_count($images_iterator));
echo '<img alt="" src="'. get_stylesheet_directory_uri() . '/images/' . $rand . '.jpg' .'">';
}
?>
</a>
</div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment