0

I'm trying to make multiple gallery posts with Flexslider. The problem is that it only renders slider first time and in the second post there is no slider. Here is the code I've got so far:

Main function to retrieve attachment images from post

<?php
    function deo_get_attachment( $num = 1 ){

      $output = '';
      if( has_post_thumbnail() && $num == 1 ): 
        $output = wp_get_attachment_url( get_post_thumbnail_id( get_the_ID() ) );
      else:
        $attachments = get_posts( array( 
          'post_type'       => 'attachment',
          'posts_per_page'  => $num,
          'orderby'         => 'none',
          'post_parent'     => get_the_ID()
        ) );
        if( $attachments && $num == 1 ):
          foreach ( $attachments as $attachment ):
            $output = wp_get_attachment_url( $attachment->ID );
          endforeach;
        elseif( $attachments && $num > 1 ):
          $output = $attachments;
        endif;

        wp_reset_postdata();

      endif;

      return $output;
    }
?>

Code to display slider:

  <?php if ( deo_get_attachment() ) : 
    $attachments = deo_get_attachment(10);
  ?>
    <div class="entry-slider">
      <div class="flexslider dots-inside">
        <ul class="slides clearfix">

          <?php foreach( $attachments as $attachment ) : ?>
            <li>
              <a href="<?php echo esc_url( get_permalink() ); ?>">
                <img src="<?php echo wp_get_attachment_url( $attachment->ID ); ?>" alt="">
              </a>
            </li>
          <?php endforeach; ?>

        </ul>
      </div>
    </div>

  <?php endif; ?>
6
  • 1
    you need to initialize them separately, via unique ids or classes. – Milo May 28 '17 at 3:19
  • Any ideas how to do it? – Alexander May 28 '17 at 3:32
  • If you google "multiple flexsliders" you'll find lots of answers. – Milo May 28 '17 at 3:39
  • If I could find solution in Google, I would not post here. The problem is all the Google solutions related to multiple ID's in jQuery. But in my case I need to initialize all sliders dynamically. And it's not a problem, in HTML you can just use single class and all the sliders will be working with this class. But in WordPress loop it doesn't work for some reason. – Alexander May 28 '17 at 3:43
  • 1
    If something works in a static html page, then it should work on a WordPress page, there's no difference on the browser side. Without seeing your complete code and how you're invoking the slider, I can't guess what the problem is. Note that javascript and 3rd party tools are off-topic here. – Milo May 28 '17 at 4:09
-1

I found my mistake. I used the same attachment images for the other post. So, WordPress can't get images that already attached to another gallery.

2
  • very unlikely... – Mark Kaplun May 28 '17 at 16:03
  • reading again the answer, your "problem" is that you can not ttach an image to more than one post – Mark Kaplun May 28 '17 at 17:34

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.