Kelvin Farrell
Kelvin Farrell

Reputation: 305

Filter posts in slider

At the minute my slider (s3Slider JQuery) calls and displays the last 5 posts. It grabs the images from a custom field labelled 'thumb'.

I'd like instead for the slider to only call images that have a value in the 'thumb' custom field. Is this possible?

Current query is...

<?php
$my_query = new WP_Query('showposts=5');
while ($my_query->have_posts()) : $my_query->the_post();
?>
<li class="sliderImage"> <a href="<?php the_permalink() ?>" rel="bookmark"> <img class="featimg" src="<?php echo get_post_meta($post->ID, 'thumb', true) ?>" alt="<?php the_title(); ?>"  />
<span class="des"><h1><?php the_title(); ?></h1><?php the_excerpt(); ?></span>    
</a>
</li>
<?php endwhile; ?>

Thanks in advance.

Upvotes: 0

Views: 218

Answers (1)

chrisn
chrisn

Reputation: 2135

You're going to want to change your WP_Query to the following (note this only works for WP >= 3.1):

$my_query = new WP_Query(
    array(
        'posts_per_page' => '5',
        'meta_query' => array(
            array(
                'key' => 'thumb',
                'value' => '',
                'compare' => '!='
            )
        )
    )
);

Of course, if someone enters a value that isn't a valid path to an image (e.g. 'blahblah'), this will still be passed to your <img> within your <li> element, so you may want to do some further checking/error handling when processing the input from the WordPress backend.

Upvotes: 1

Related Questions