Function to show random posts from a category

Hi to all I’m trying to create a function that can ECHO the excerpt of a random post from a choosen category.

What I did up to now is:

<?php function $titoloslide() = {
query_posts(array('orderby' => 'rand', 'category_name' => 'testimonianze', 'showposts' => 1)); if (have_posts()) : while (have_posts()) : the_post();
"<div class="testimonianzefurgonihome"><a href="#" title="Testimonianze noleggio furgoni KobalRent">"the_excerpt();"</a></div>"
 endwhile;
endif;
wp_reset_query();};?>

Obviously… it doesn’t work!
Is there anyone who can help me?
Thank you very much!

Why i need all of this inside a function?
Because I need to use it as title tag of some images and stuff like this so I can’t write all my code inside title=”” and I tought that a function could do the trick.

I’m trying to use NivoSlider so I need to put this code as a title tag to an image to be shown.

example:
” title=” />

This function takes a random post from category testimonianze and shows his excerpt (I think it’s easy to understan this 😛

The code works if I use it like this:

<?php query_posts(array('orderby' => 'rand', 'category_name' => 'testimonianze', 'showposts' => 1)); if (have_posts()) : while (have_posts()) : the_post(); ?>
<div class="testimonianzefurgonihome"><a href="#" title="Testimonianze noleggio furgoni KobalRent"><?php the_excerpt(); ?></a></div>
 <?php endwhile; ?>
 <?php endif;?>
 <?php wp_reset_query(); ?>

But no way to have a working version in any other way even after code corrections.

Any idea or suggestions? (Maybe some prays will work too 😛 )

1 Answer
1

cleaned up syntax errors:

<?php function titoloslide() { 
query_posts(array(
  'orderby' => 'rand', 
  'category_name' => 'testimonianze', 
  'posts_per_page' => 1
)); 
if (have_posts()) : while (have_posts()) : the_post(); ?>
 <div class="testimonianzefurgonihome"><a href="#" title="Testimonianze noleggio furgoni KobalRent"><?php the_excerpt(); ?></a></div>
<?php endwhile; endif; wp_reset_query(); 
}
?>

Leave a Comment