Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

WP Loop only working on Index.php ?? WordPress

  • SOLVED

Hi,

I am a noob to Wordpress, and have ran into a small difficulty.

My problem is, I have a Loop in my footer.php file , which <strong>shows 3 posts in a random order</strong>, which is shown below :


<?php $posts = get_posts("numberposts=3&category_name=graphics,branding,misc,webdesign&orderby=rand" ); ?>


<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>


<div id="left-column">

<?php if ( (function_exists('has_post_thumbnail')) && (has_post_thumbnail()) ) : ?>

<div class="post-thumb post-lead">
<a title="<?php printf(__('Go to %s', 'framework'), get_the_title()); ?>" href="<?php the_permalink(); ?>"><?php the_post_thumbnail('home-thumb'); /* post thumbnail settings configured in functions.php */ ?></a>
</div>
<?php endif; ?>

<?php the_excerpt(); ?>

</div>

<?php endwhile; else: ?>
<p><?php _e('Sorry, no posts matched your criteria.'); ?></p>
<?php endif; ?>




This works like a charm on my Index page when the footer is called, but whenever the footer is being called on another page template, the loop <strong>only shows 1 random post instead of 3</strong>...

Any Help?

Regards,

Neal

Answers (2)

2011-05-24

Christianto answers:

Hi Neal,

get_posts will return an object, its not change any global query

<?php $posts = get_posts("numberposts=3&category_name=graphics,branding,misc,webdesign&orderby=rand" ); ?>
<?php foreach($posts as $footerpost) {?>
<div id="left-column">
<?php if ( (function_exists('has_post_thumbnail')) && (has_post_thumbnail()) ) : ?>
<div class="post-thumb post-lead">
<a title="<?php printf(__('Go to %s', 'framework'), $footerpost->title); ?>" href="<?php echo get_permalink($footerpost->ID); ?>"><?php the_post_thumbnail('home-thumb'); /* post thumbnail settings configured in functions.php */ ?></a>
</div>
<?php endif; ?>
<?php echo $footerpost->post_excerpt; ?>
</div>
<?php } ?>

I'm not sure about the_post_thumbnail function is this custom function?


or you can create a new query for the loop..

<?php $posts = new WP_Query("numberposts=3&category_name=graphics,branding,misc,webdesign&orderby=rand" ); ?>
<?php if ( $posts->have_posts() ) : while ( $posts->have_posts() ) : $posts->the_post(); ?>
<div id="left-column">
<?php if ( (function_exists('has_post_thumbnail')) && (has_post_thumbnail()) ) : ?>
<div class="post-thumb post-lead">
<a title="<?php printf(__('Go to %s', 'framework'), get_the_title()); ?>" href="<?php the_permalink(); ?>"><?php the_post_thumbnail('home-thumb'); /* post thumbnail settings configured in functions.php */ ?></a>
</div>
<?php endif; ?>
<?php the_excerpt(); ?>
</div>

<?php endwhile; else: ?>

<p><?php _e('Sorry, no posts matched your criteria.'); ?></p>

<?php endif; ?>


Neal Mullen comments:

Christianto .... you absolute life saver! Thank you so much!


Christianto comments:

Ok, the_post_thumbnail function is wordpress function so if you use get_posts instead of creating new query please change my first code to

<?php $posts = get_posts("numberposts=3&category_name=graphics,branding,misc,webdesign&orderby=rand" ); ?>
<?php foreach($posts as $footerpost) {?>
<div id="left-column">
<?php if ( (function_exists('has_post_thumbnail')) && (has_post_thumbnail($footerpost->ID)) ) : ?>
<div class="post-thumb post-lead">
<a title="<?php printf(__('Go to %s', 'framework'), $footerpost->title); ?>" href="<?php echo get_permalink($footerpost->ID); ?>"><?php the_post_thumbnail($footerpost->ID,'home-thumb'); /* post thumbnail settings configured in functions.php */ ?></a>
</div>
<?php endif; ?>
<?php echo $footerpost->post_excerpt; ?>
</div>
<?php } ?>

Let me know if it works, and thumbnail is appear..
Thanks

2011-05-24

Kristin Falkner answers:

You probably need to make use of

<?php wp_reset_query(); ?>

Right after your main post loops on category, archive, etc. pages


Neal Mullen comments:

I've tried that, pretty much everywhere possible, but unfortunately that doesnt seem to be the fix :(