Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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 )

share|improve this question
    
Update your answer with a little more code, to show how you're currently using the function, and in which theme file. –  TheDeadMedic Jun 17 '11 at 20:30

1 Answer 1

up vote 1 down vote accepted

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(); 
}
?>
share|improve this answer
    
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? –  Downloadtaky Jun 17 '11 at 20:11
    
how do you call the function in your template? like so: <?php titoloslide(); ?> ? –  Michael Jun 17 '11 at 21:04
    
I'm using: <img src="img1.jpg" title="<?php echo $titoloslide;?>" /> –  Downloadtaky Jun 17 '11 at 21:44
    
If I use this: <?php titoloslide(); ?> inside title tag I can see: <p> bla bla bla </p> Any solution? –  Downloadtaky Jun 17 '11 at 21:46
    
lol now it is working.... it was only a mistake on my side... you are the BEST! –  Downloadtaky Jun 17 '11 at 22:04

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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