Support

Account

Forum Replies Created

  • This did the trick! Cheers guys. Not sure who to credit but Johns response was just a tiny bit earlier 🙂

  • Hi John,

    So there’s no more warning, and if I print_r on attachments I get:

    
    Array ( [0] => Array ( [title] => Image 0 [image] => Array ( [attachment_id] => 1653 ) ) [1] => Array ( [title] => Image 1 [image] => Array ( [attachment_id] => 1652 ) ) [2] => Array ( [title] => Image 2 [image] => Array ( [attachment_id] => 1651 ) ) [3] => Array ( [title] => Image 3 [image] => Array ( [attachment_id] => 1650 ) ) [4] => Array ( [title] => Image 4 [image] => Array ( [attachment_id] => 1649 ) ) [5] => Array ( [title] => Image 5 [image] => Array ( [attachment_id] => 1648 ) ) )
    

    It looks like the correct data is being returned but there are still no images loading in gallery field. Just checking that I’ve understood you correctly, this is the ammended snippet:

    
    function build_gallery_from_rs($value, $post_id, $field) {
      if (!empty($value)) {
        // already has a value
        return $value;
      }
      // does not have a value, build value from slider attachments
      global $wpdb;
      $table = $wpdb->prefix . 'new_royalsliders';
      $id = get_field('media_gallery_slider', $post_id, false);
      $slider_data = $wpdb->get_results( $wpdb->prepare("SELECT * FROM $table WHERE id=%d", $id), ARRAY_A );
      
      // Build image array
      //$value = array();
      if ($slider_data) {
    
        foreach ($slider_data as $slide) {
          $attachments = json_decode($slide['slides'], true);
          //print_r($attachments);
          if ($attachments) {
            foreach ($attachments as $attachment) {
              $value = $attachment['attachment_id'];
            }
          }
        }
    
      }
      return($value);
      
    }
    

    Many thanks again!

  • This is giving a Warning: json_decode() expects parameter 1 to be string. If I print the result of $slider_data:

    
    Array ( 
      [0] => Array ( 
        [id] => 47 
        [active] => 1 
        [type] => custom 
        [name] => Slider Title 
        [skin] => myCustomSkin 
        [template] => simple_vertical 
        [slides] => [
          {
           "title":"Image 1",
           "image":{"attachment_id":"1653"}
          },
          {
            "title":"Image 2",
            "image":{"attachment_id":"1648"}
          }
        ] 
        [options] => {
          // A whole bunch of options
        } 
        [template_html] =>
          {{image_tag}} {{thumbnail}} {{html}} {{animated_blocks}} {{#link_url}} {{title}} {{/link_url}}
      ) 
    )
    

    Printing $value returns:

    
    Array ( )
    

    I only need what’s in the slides array. Does this change how the array gets traversed?

  • Hi John,

    Many thanks for the response!

    What I’m trying to do is take the array of images from a posts slider Id so that I can populate the gallery field and eventually remove the royalslider plugin as a dependency. There are hundreds of posts so I didn’t want to have to create each gallery from scratch.

    Here’s an example of how the royalslider plugin stores an array in its ‘slides’ column in the database:

    
    [{"title":"Slider 47","image":{"attachment_id":"1653"}},{"title":"Image-1","image":{"attachment_id":"1652"}},{"title":"Image-2","image":{"attachment_id":"1651"}},{"title":"Image-3","image":{"attachment_id":"1650"}},{"title":"Image-4","image":{"attachment_id":"1649"}},{"title":"Image-5","image":{"attachment_id":"1648"}}]
    

    Using your ammendments I can now link to the correct slider ID but images are not being loaded into the gallery field:

    
    add_filter('acf/load_value/key=field_5e2058d65e519', 'build_gallery_from_rs', 10, 3);
    
    function build_gallery_from_rs($value, $post_id, $field) {
      if (!empty($value)) {
        // already has a value
        return $value;
      }
      // does not have a value, build value from slider attachments
      global $wpdb;
      $table = $wpdb->prefix . 'new_royalsliders';
      $id = get_field('media_gallery_slider', $post_id, false);
      $slider_data = $wpdb->get_results( $wpdb->prepare("SELECT * FROM $table WHERE id=%d", $id), ARRAY_A );
      // How to build image array?
      $media = $slider_data[0];
      $value = array();
      if ($media) {
        foreach ($media as $image) {
          $value[] = $image->ID;
        }
      }
      return $value;
    }
    
  • Ok, I figured this out. Needed to change orderby to ‘orderby’ => ‘post__in’

      if($ids){
      $args = array(
          'post_type' => 'any',
          'posts_per_page' => 6,
          'post__in' => $ids,
          'post_status' => 'publish',
          'orderby' =>  'post__in',
          'order' => 'ASC'
      );
      } else{
          $args = array();
      }
  • This is great John, thank you for sharing. I’ll have a look and if I have any questions I’ll ping you on github. Many thanks again!

  • Hi John

    You’re probably right regarding the suitability of this feature with ACF. I’d love to see how you achieved your solution in a gist – if only for learning purposes 🙂 I can’t see me using this solution too often either. The only reason I started looking for one was because somebody wanted a friggin parallaxing homepage and I was curious as to how to add an easy solution to change the images used.

  • Hi John

    I’m looking for a solution that can dynamically change the inline style tag to set the path to the appropriate background image. I’ve managed to do this with feature thumbnail images within <picture></picture> using picturefill as the polyfill but cannot find any reference to a background images solution. It would be nice if this sort of thing would be a nice feature for ACF using an image array without having to resort to multiple uploads? Hoping Elliot feels the same 🙂

  • Figured it out. Using this:

    <?php $var = get_field('media_gallery_slider');
    if ($var == '')
    { }
    else { echo do_shortcode( '[new_royalslider id="' . $var . '"]'); } ?>
  • Hi spacewindow

    I’m just using this snippet to return a list of RoyalSliders in a select field for a custom post type. Works perfectly in the backend. How did you get the slider to display on the front end? When using the_field('media_gallery_slider'); I just get the slider ID number displayed. I’m assuming you manage to echo the shortcode on the front end, but can’t figure out how? Any help would be awesome

    Many thanks!

Viewing 10 posts - 1 through 10 (of 10 total)