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.

Hiya Guys! so I need some help with this:

            <?php

        $newsposts = new WP_Query('cat=restaurant');

        if ( is_front_page()) { echo '<h3 class="member-review">Latest Restaurants</h3>

        <div id="extra">if ($newsposts->have_posts()) : while ($newsposts->have_posts()) : $newsposts->the_post();

        <div class="reslogo"><img src="'echo catch_that_image()' /></div>

        endwhile, endif; 

        </div>';    

        } ?>

What I'm trying to do here is have an image slider of first post images (from restaurant category) display at the bottom of the page. I use DIVs to control placement/ style but I really need help with my PHP syntax ... I just cant figure it out... Any help would be greatly appreciated!

Cheers

share|improve this question
add comment

2 Answers

up vote 1 down vote accepted

you can't run code inside quote since php treats it as string, try this:

<?php
    $newsposts = new WP_Query('cat=restaurant');
    if ( is_front_page()) { 
        echo '<h3 class="member-review">Latest Restaurants</h3>
        <div id="extra">';
        if ($newsposts->have_posts()) : while ($newsposts->have_posts()) : $newsposts->the_post();
            echo '<div class="reslogo"><img src="'.catch_that_image().'"/></div>';
        endwhile, endif; 
        echo '</div>';    
    } 
?>
share|improve this answer
    
Hey man thank you for your suggestion I tried both approaches and found yours worked exactly as I needed - just needed to replace the endwhile, endif; with endwhile; endif; one of these cats (;) you guys are great I really enjoy learning code and its so awesome that there are people like you out there helping guys like me ;) Cheers - ave a great sunday! –  brett Apr 3 '11 at 14:04
    
Glad i could help. –  Bainternet Apr 3 '11 at 14:06
    
Hi there, Is there a way that I can limit the amount of posts being displayed? - I'd just like to have seven. I've tried this: code<?php $newsposts = new WP_Query('cat=restaurant'.'limit=7'); if ( is_front_page()) { echo '<h3 class="member-review">Latest Restaurants</h3> <div id="homeslider">'; if ($newsposts->have_posts()) : while ($newsposts->have_posts()) : $newsposts->the_post(); echo '<div class="reslogohome"><img src="'.catch_that_image().'"/></div>'; endwhile; endif; echo '</div>';} ?>code –  brett Apr 4 '11 at 6:57
add comment

I would suggest a totally different syntactic approach: instead of echoing HTML from PHP, insert PHP calculated values inside HTML. This makes the code more organized and readable.

<?php $newsposts = new WP_Query('cat=restaurant'); ?>

<?php if ( is_front_page()): ?>
<h3 class="member-review">Latest Restaurants</h3>

<div id="extra">
    <?php if ($newsposts->have_posts()): ?>
        <?php while ($newsposts->have_posts()) : $newsposts->the_post(); ?>

        <div class="reslogo"><img src="<?php echo catch_that_image() ?>" /></div>

        <?php endwhile; ?>
    <?php endif; ?>
</div>';    

Note: I don't know if this code is correct, I just updated the syntax.

share|improve this answer
    
Thank you for having a look and I really liked your suggestion. Especially in your approach to the code. I appreciate your time! Have a super sunday –  brett Apr 3 '11 at 14:03
add comment

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.