2

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 :P

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 :P )

1
  • Update your answer with a little more code, to show how you're currently using the function, and in which theme file. Commented Jun 17, 2011 at 20:30

1 Answer 1

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(); 
}
?>
6
  • THX a lot! Ok, now my code is correct... but it doesn't work :-( Like this it works: <?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 inside this function...not, Where is the mistake? Commented Jun 17, 2011 at 20:11
  • how do you call the function in your template? like so: <?php titoloslide(); ?> ?
    – Michael
    Commented Jun 17, 2011 at 21:04
  • I'm using: <img src="img1.jpg" title="<?php echo $titoloslide;?>" /> Commented Jun 17, 2011 at 21:44
  • If I use this: <?php titoloslide(); ?> inside title tag I can see: <p> bla bla bla </p> Any solution? Commented Jun 17, 2011 at 21:46
  • lol now it is working.... it was only a mistake on my side... you are the BEST! Commented Jun 17, 2011 at 22:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.