0

I have a website that displays a slideshow using custom fields and the Slick carousel, using the code below.

    <?php $entries =  get_post_meta( get_the_ID(), '_mysite_homepage_slider_group', true );

    foreach ( (array) $entries as $key => $entry ) {

            $img = $title = $url = $desc = '';

            if ( isset( $entry['_mysite_homepage_slider_title'] ) )
                $title = esc_html( $entry['_mysite_homepage_slider_title'] );

            if ( isset( $entry['_mysite_homepage_slider_caption'] ) )
                $desc = wpautop( $entry['_mysite_homepage_slider_caption'] );

            if ( isset( $entry['_mysite_homepage_slider_url'] ) )
                $url = esc_html( $entry['_mysite_homepage_slider_url'] );

            if ( isset( $entry['_mysite_homepage_slider_image_id'] ) ) {
                $img = wp_get_attachment_image_url( $entry['_mysite_homepage_slider_image_id'], 'full');
            }

        } ?>

I'd like the user to be able to disable individual slides by clicking a checkbox in the back-end.

Simple enough, right?

So, I added a checkbox and came up with this code to drop in to my snippet. It checks to see if the checkbox is clicked. I tried placing it immediately after the foreach line, like this:

    foreach ( (array) $entries as $key => $entry ) {

        // Display slides if checkbox is NOT clicked (e.g., is empty)
        if ( empty( $entry['_mysite_homepage_slider_checkbox'] ) ) {

           $img = $title = $url = $desc = '';

     (...)
     }

It kind of works — the slide doesn't show up on the page — but something's still getting through. A duplicate slide is being created.

For example, There are 4 slides in the back end. The 4th has a checkbox that is checked. On the front end, Slide #3 is displayed twice. It appears the code is outputting this:

  • Slide 1
  • Slide 2
  • Slide 3
  • Slide 3 (again!)

Where have I gone wrong?

EDIT: Final code snippet with answer, for anyone who may find this useful in the future.

2 Answers 2

0

There's some code missing from your question - the code where your slides are actually output.

The foreach loop you've posted only sets the variables, and the actual output happens below the loop.

So, all you're doing on the fourth slide is avoiding setting the variables... hence the slide is still output, but using the third slide's variables.

Your if checkbox logic looks ok; you just need to include the logic on the actual code that outputs the slides too.

2
  • Thanks, Tim. Moving my if checkbox to the code that outputs the slide did it. I had assumed that by having it in that initial block if wouldn't be output at all.
    – SPS
    Jul 12, 2016 at 0:41
  • @SPS No problem, glad that helped. Yeah never assume it's going to do what you think.. gotta test, test, test and explicitly define your instructions ;) In this case, avoiding setting the variables just means that they're not overwritten from the last iteration of the loop. Even if that wasn't the case, running the slider code without setting variables that it needs would usually result in errors.
    – Tim Malone
    Jul 12, 2016 at 0:42
-1

Are all the if-statements that output html actually inside the checkbox if-statement? Can't really tell from the way you've posted the code. It looks like maybe on the last trip through the foreach loop, the values of the variables are being remembered from the previous trip and outputting them again??

2
  • 1
    Please do not use comments to ask for clarity or debugging info. Answers should actually solve the issue. Please see How to Answer Jul 11, 2016 at 18:29
  • Hmm. Seems like I wasn't able to leave a "comment" yesterday (Said something like I didn't have enough reputation points. No worries. Will do.
    – slashbob
    Jul 12, 2016 at 19:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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