If custom field has value, show in slidehow. Else hide

I’m using WordPress and the PODS plugin and created the pod project. In project i’ve created a set of fields. One of those is project_slide. If this field has a value, I want to show this in a RoyalSlider on my frontpage.

I’ve started with this piece of code (= one slide).

Read More
<?php
    $project = pods('project', array('limit' => -1));
    while ($project->fetch()) {

        echo '<div class="rsContent">';
        echo    '<img class="rsImg" src="' . $project->display('project_slide') . '" alt="' . $project->display('project_title') . '">';
        echo '</div>';
    }
?>

The problem is: each project I add, displays in the slider. Even if project_slide has no value. Any idea how to resolve this?

Related posts

Leave a Reply

1 comment

  1. Just add an if-statement to the loop.
    Hide Image example
    In my example, I only add the slide when the value isn’t [empty string]

    while ($project->fetch()) {
        if( $project->display('project_slide') !== ""){
            echo '<div class="rsContent">';
            echo    '<img class="rsImg" src="' . $project->display('project_slide') . '" alt="' .$project->display('project_title') . '">';
            echo '</div>';
        }
    }
    

    Expand with a detection if the image actually exists
    You could expand it with a file_exists to also check if it exists (for the items with value, but the image itself doesnt exists). Just make sure the file_exists is 2nd, that way it only checks if the file is present if the string is not empty (if-statements go from left to right)

    $project->display('project_slide') !== "" && file_exists($_SERVER['DOCUMENT_ROOT'].$project->display('project_slide')`
    

    Default image method:
    Instead of showing it only when there is an image, you could always show it, but build a default-image logic:

    $defaultImage = '/some/image.jpg'; // set a default image outside of the loop
    while ($project->fetch()) {
        $src= $project->display('project_slide') !== "" ? $project->display('project_slide') : $defaultImage; // set a default umage
        echo '<div class="rsContent">';
        echo    '<img class="rsImg" src="' . $src . '" alt="' .$project->display('project_title') . '">';
        echo '</div>';
    }