2

So within my Customizer I have a section that controls this Slider on the Homepage - the thing the client might not want to use all the Slider Images but all the Sliders still show anyway and the ones without an Image just show the alt tag.

Is there a way to basically check if there is a image and if not don't display that Slide?

EDIT: I'm trying to achieve something similar to post_thumbnail as in if there is an image then displays it but if there isn't one then don't display it at all.

So this is how I get the Slider mage:

<div class="item active">
    <img class="img-responsive" src="<?php echo esc_url( get_theme_mod( 'slider_one' ) ); ?>" alt="Slider 1">
</div>

Then I register this in my Customizer.php:

/**
 * Adding the Section for Slider
 */
$wp_customize->add_section('slideshow', array(
    'title'             => __('Slider Images', 'patti-theme'), 
    'priority'          => 60,
));

/**
 * Adding the Settings for Slider
 */
$wp_customize->add_setting('slider_one', array(
    'transport'         => 'refresh',
    'height'            => 525,
));  

/**
 * Adding the Controls for Slider
 */
$wp_customize->add_control( new WP_Customize_Image_Control( $wp_customize, 'slider_one_control', array(
    'label'             => __('Slider Image #1', 'patti-theme'),
    'section'           => 'slideshow',
    'settings'          => 'slider_one',    
)));

So is there a way to check if slider_one has a Image inserted and if not don't display it.

What is shown if there is no Image inserted:

enter image description here

They then click Select Image and shows the Media Library where they can pick and Image or even Upload a new Image

How the Slider outputs the Image if there is no Image:

enter image description here

I don't want this Slider to display at all if the src element inside the img tag is empty.

The Slider outputs the Image with this line of code:

<img class="img-responsive" src="<?php echo esc_url( get_theme_mod( 'slider_one' ) ); ?>" alt="Slider 1">
7
  • Yeah a bit like post thumbnails so if it has_post_thumbnail it displays but if it doesn't then it doesn't display at all and that slide of the slider is just invisible
    – Stephen
    Commented Feb 11, 2016 at 9:47
  • I'm not too sure what you mean by this - it's not an actual thumbnail it's a slider but I want to check if there is an image attached to say slider_one and if there isn't then that slider is deactivated and doesn't display at all
    – Stephen
    Commented Feb 11, 2016 at 9:50
  • How do you setup your slider. How and what determine the slider. I see you use get_theme_mod(). Commented Feb 11, 2016 at 9:53
  • Yeah I've just updated my question hopefully this gives you more of a insight of what I'm doing
    – Stephen
    Commented Feb 11, 2016 at 9:55
  • I can understand that, yes. What we will need to now is, as user input, what do you pass to your customizer field, what will the user input into that field. If it is a post ID, image ID or link. If it is, say a post ID or an image ID, how is it processed and how does the output finally look from get_theme_mod Commented Feb 11, 2016 at 10:06

2 Answers 2

3

Try the following code:

<div class="item active <?php
    // If image URL is empty, echo "hidden"
    echo (get_theme_mod('slider_one', '') == '' ? 'hidden' : '');
?>">
    <img class="img-responsive" src="<?php echo esc_url( get_theme_mod( 'slider_one', '' ) ); ?>" alt="Slider 1">
</div>

Then use the following CSS:

.hidden { display: none }

What this will do is:

  • If no image is selected (i.e. get_theme_mod(...) == '')
  • Echo class hidden
  • The CSS will hide all elements with .hidden
1
  • So this worked exactly how I wanted it to :) Cheers for the help
    – Stephen
    Commented Feb 11, 2016 at 12:57
2

Just do a conditional check slightly earlier to wrap the div?

<?php if ( get_theme_mod( 'slider_one', '') != '') : ?>
<div class="item active">
    <img class="img-responsive" src="<?php echo esc_url( get_theme_mod( 'slider_one', '' ) ); ?>" alt="Slider 1">
</div>
<?php endif; ?>
3
  • This doesn't seem to work - it still shows the slide. if there is no image inserted into the field in the customizer than I want that complete Slider to disappear
    – Stephen
    Commented Feb 11, 2016 at 11:48
  • Not sure why it's not working for you, the div should should even be output in that case so why the slide would show I don't know. guess that is how the slider is setup somehow. ah well.
    – majick
    Commented Feb 12, 2016 at 11:06
  • It work as in it didn't show the alt tag but it still displayed the slide but then @Druzion gave me his answer and by echoing the hidden class that then worked, thanks for your help though I appreciate it :)
    – Stephen
    Commented Feb 12, 2016 at 11:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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