Eric Wood
Eric Wood

Reputation: 589

Creating Conditional Script in PHP / Wordpress

I'm still pretty new to PHP, and I'm having trouble getting this to work. What I want to do, is make the slide linked (if link is available). Otherwise, print the post thumbnail without the

Here's my code so far:

<?php // START SLIDER ?>
<div class="slider">
<ul class="rslides">
<?php $args = array( 'posts_per_page' => 0, 'post_type' => 'slide'); $alert = new WP_Query( $args ); ?>
<?php if( $alert->have_posts() ) { while( $alert->have_posts() ) { $alert->the_post(); ?>
<li><a href="<?php echo get_post_meta($post->ID, "_location", true); ?>" title="More Info"><?php the_post_thumbnail('full'); ?><div class="caption"><p class="captiontitle"><?php the_title(); ?></p><p class="caption"><?php the_content(); ?></p></div></a></li> 
<?php } } ?>
</ul>
</div>
<?php wp_reset_query(); ?>
<?php // END SLIDER ?>

I've done this before using the WP Custom fields, but I'm not sure how to apply it to my custom post type (called slider). Here's what I did for my Custom Field script:

<?php $slider_url = get_post_meta($post->ID, 'Slider_URL', true);
if ($slider_url) { ?>

LINKED SLIDE HERE

<?php } else { ?>

UNLINKED SLIDE HERE

<?php } ?>

<?php endwhile; ?>
<?php endif; // have_posts() ?>

Here's what I tried (when combining the two), but there's an error somewhere:

<?php // START SLIDER ?>
<div class="slider">
<ul class="rslides">
<?php $args = array( 'posts_per_page' => 0, 'post_type' => 'slide'); $alert = new WP_Query( $args ); ?>
<?php if( $alert->have_posts() ) { while( $alert->have_posts() ) { $alert->the_post(); ?>

<?php $slide_url = get_post_meta($post->ID, 'Slide_URL', true);
if ($slide_url) { ?>

<li><a href="<?php echo get_post_meta($post->ID, "_location", true); ?>" title="More Info"><?php the_post_thumbnail('full'); ?><div class="caption"><p class="captiontitle"><?php the_title(); ?></p><p class="caption"><?php the_content(); ?></p></div></a></li>

<?php } else { ?>

<li><?php the_post_thumbnail('full'); ?><div class="caption"><p class="captiontitle"><?php the_title(); ?></p><p class="caption"><?php the_content(); ?></p></div></li>

<?php } } ?>

<?php endwhile; ?>
<?php endif; // have_posts() ?>

</ul>
</div>
<?php wp_reset_query(); ?>
<?php // END SLIDER ?>

Upvotes: 0

Views: 96

Answers (1)

Daryll Doyle
Daryll Doyle

Reputation: 453

If I'm correct in thinking, you just want to check if the link is there, before outputting, otherwise, just show the image. Try the following:

<?php // START SLIDER ?>
<div class="slider">
    <ul class="rslides">
        <?php $args = array( 'posts_per_page' => 0, 'post_type' => 'slide'); $alert = new WP_Query( $args ); ?>
        <?php if( $alert->have_posts() ) { while( $alert->have_posts() ) { $alert->the_post(); ?>
        <!-- Get a link -->
        <?php $theLink = get_post_meta($post->ID, "_location", true); ?>
        <li>
            <!-- Check for a link -->
            <?php if($theLink != ''): ?>
                <a href="<?php echo $theLink; ?>" title="More Info">
            <?php endif; ?>
                <?php the_post_thumbnail('full'); ?>
                <div class="caption">
                    <p class="captiontitle">
                        <?php the_title(); ?>
                    </p>
                    <p class="caption">
                        <?php the_content(); ?>
                    </p>
                </div>
            <!-- Close the link -->
            <?php if($theLink != ''): ?>
                </a>
            <?php endif; ?>
        </li> 
        <?php } } ?>
    </ul>
</div>
<?php wp_reset_query(); ?>
<?php // END SLIDER ?>

Upvotes: 1

Related Questions