Basically, I’m showing one random post from a specific category, and then having a list under that which shows recent posts, not including whatever random post was displayed earlier.
This is what I have (just an excerpt from the full code, so don’t pay any mind to the extra closing div tags):
<div class="mainLinkColumn">
<?php $randomPosts = new WP_Query("cat=16&posts_per_page=1&orderby=rand"); while($randomPosts->have_posts()) : $randomPosts->the_post();?>
<div class="module big portfolio" style="background: url(<?php echo featured_portfolio(); ?>) no-repeat center center; background-size: cover">
<h2><a href="portfolio">Portfolio</a></h2>
<a href="<?php the_permalink(); ?>" class="moduleClick"></a>
<a class="more-link" href="<?php the_permalink(); ?>"><?php the_title_attribute(); ?></a>
<?php endwhile; ?>
</div>
</div>
</div>
<ul class="recentPosts">
<?php
$args=array(
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => 8,
'post__not_in' => array($post->ID)
);
$my_query = null;
$my_query = new WP_Query($args);
if( $my_query->have_posts() ) {
echo '';
while ($my_query->have_posts()) : $my_query->the_post(); ?>
<li class="box1" style="background-image: url(<?php echo get_first_image() ?>)">
<a href="<?php the_permalink(); ?>">
<?php if ( in_category( 'Portfolio' )) { ?>
<span>
<?php the_title(); ?>
</span>
<?php } else { ?>
<span>
<?php the_title(); ?>
</span>
<time datetime="<?php the_time('c'); ?>" class="recentDate"><?php the_time('m/d/y') ?></time>
<?php } ?>
</a>
</li>
<?php
endwhile;
}
wp_reset_query();
?>
</ul>
I’m still learning PHP (which is probably relatively obvious), so I’m wondering if this is okay. It works, but I’m not sure if having two WP_query’s like that is going to interfere with anything.