Support

Account

Home Forums Add-ons Gallery Field Custom Fields in Gallery Array

Solved

Custom Fields in Gallery Array

  • Hello,

    I was wondering if there was a way to add custom attachment fields to the array of data returned from the Gallery plugin. Through the ACF plugin I added a checkbox to my attachments. I would like to be able to pass that value into the images piece of the array returned from calling the gallery. The current gallery only returns these parameters for each image:

    Array
    (
        [id] => 540
        [alt] => A Movie
        [title] => Movie Poster: UP
        [caption] => sweet image
        [description] => a man and a baloon
        [url] => http://localhost:8888/acf/wp-content/uploads/2012/05/up.jpg
        [sizes] => Array
            (
                [thumbnail] => http://localhost:8888/acf/wp-content/uploads/2012/05/up-150x150.jpg
                [medium] => http://localhost:8888/acf/wp-content/uploads/2012/05/up-300x119.jpg
                [large] => http://localhost:8888/acf/wp-content/uploads/2012/05/up.jpg
                [post-thumbnail] => http://localhost:8888/acf/wp-content/uploads/2012/05/up.jpg
                [large-feature] => http://localhost:8888/acf/wp-content/uploads/2012/05/up.jpg
                [small-feature] => http://localhost:8888/acf/wp-content/uploads/2012/05/up-500x199.jpg
            )
     
    )
    

    Along with id, alt, description, etc I would like to be able to have data for a custom field called ‘constrain’ for each image [constrain] = > true which is already set up on my images through ACF.

    How can I go about doing that?

  • you could just call it like:

    
    <?php
    $constrain = get_field('constrain', $image['id']); //$image being the single image in the gallery array
    
    ?>
    
    

    Since it’s basically just another meta-data for a post.

  • Thanks. I had tried this before, but the problem is that I am creating a javascript array from the data. If I were to use what you intended I would need to be able to do the equivalent from JS rather than php. That’s why I thought it would be easier to put the data in the array rather than making multiple ajax requests.

    Below is the function that is called via ajax. The function returns a json object that I then pull the values from and apply to a mustache template.

    			function buildGallery(){
    				$post_title = $_REQUEST['post_title'];
    				if($post_title){
    					$work = new WP_Query( array(
    				        'post_type' => 'work',
    				        'post_status' => 'publish',
    				        'numberposts' => '-1',
    				        'order' => 'asc',
    				        'name' => $post_title 
    				    	)
    				    );
    				    foreach ($work->posts as $work_post) {
    			        	$post_id = $work_post->ID;
    			    	}
    				}else{
    					//get the data from ajax() call
    					$post_id = $_REQUEST['post_id'];
    				}
    				$gallery_item = array(
    			        "gallery_item" => array()
    			    );
    
    				global $post;
    				$post = get_post($post_id);
    
    				$previous_post = get_previous_post();
    				$next_post = get_next_post();
    				array_push($gallery_item['gallery_item'], array(
    		            "images" => get_field('gallery', $post_id),
    		            "title" => get_the_title($post_id),
    		            "content" => apply_filters( 'the_content', get_post_field('post_content', $post_id)),
    		            "nextLink" => $next_post,
    		            "prevLink" => $previous_post,
    		            "id" => $post_id,
    		            "postName" => $post->post_name,
    		            "company" => get_field('company', $post_id),
    		            "tags" => wp_get_post_tags($post_id)
    		        ));
    				// Return the String
    				die(json_encode($gallery_item));
    			}

    The images are added here: “images” => get_field(‘gallery’, $post_id),

    I am having a hard time getting my head around how to add the other field into that piece of the array.

  • @Kevin

    I see.. I think you should do something like this.. Assuming that I fetch the image ID correctly from the images array this should work 🙂

    
    <?php
    function buildGallery(){
    	$post_title = $_REQUEST['post_title'];
    	if($post_title){
    		$work = new WP_Query( array(
    	        'post_type' => 'work',
    	        'post_status' => 'publish',
    	        'numberposts' => '-1',
    	        'order' => 'asc',
    	        'name' => $post_title 
    	    	)
    	    );
    	    foreach ($work->posts as $work_post) {
            	$post_id = $work_post->ID;
        	}
    	}else{
    		//get the data from ajax() call
    		$post_id = $_REQUEST['post_id'];
    	}
    	$gallery_item = array(
            "gallery_item" => array()
        );
    
    	global $post;
    	$post = get_post($post_id);
    
    	$previous_post = get_previous_post();
    	$next_post = get_next_post();
    	
    	//Fetch the gallery for the selected post
    	$gallery = get_field('gallery', $post_id);
    	if($gallery){
    		foreach($gallery as $image){ //loop through the images in the gallery
    			$constrain = get_field('constrain', $image['id']); //fetch our custom field
    			$image['constrain'] = $constrain; //Add it to the image array
    		}
    	}
    	
    	array_push($gallery_item['gallery_item'], array(
            "images" => $gallery, //output the new modified gallery array
            "title" => get_the_title($post_id),
            "content" => apply_filters( 'the_content', get_post_field('post_content', $post_id)),
            "nextLink" => $next_post,
            "prevLink" => $previous_post,
            "id" => $post_id,
            "postName" => $post->post_name,
            "company" => get_field('company', $post_id),
            "tags" => wp_get_post_tags($post_id)
        ));
    	// Return the String
    	die(json_encode($gallery_item));
    }
    
    ?>
    
    
  • Wow, thanks! I thought this was going to work, and it has to be close. It appears that it isn’t even getting into the foreach loop. I hardcoded a value for an attachment and also just tried to get it to append the ‘constrain’ property, but no luck.

    When the array comes back it looks like the attached image (this was without modifying your code at all).

  • Aaaahh..

    It seems that the returned data is an object rather than an array..I’m not sure wether this is due to your json encoding or not tho. When checking the documentation it’s clearly an array and not an object..

    If this is what you get back using my code it does seem to run through the foreach since you do get something in “gallery” the issue seems to be that it does not add constrain which it should.

  • I decided to take a step back and try to hard code an example that would work for one image. I successfully got this to work:

    $gallery[0][‘constrain’] = get_field(‘constrain’, $gallery[0][‘id’]);

    Now I need to figure out how increment that ‘0’ in the loop for each image I have. If you have any ideas, let me know. And thanks so much for all of your help!

  • Ok, it appears to be working. I changed your loop to look like this:

    //Fetch the gallery for the selected post
    $gallery = get_field('gallery', $post_id);
    if($gallery){
    	$i = 0;
    	foreach($gallery as $image){ //loop through the images in the gallery
    		
    		$gallery[$i]['constrain'] = get_field('constrain', $gallery[$i]['id']);
    		$i++;
    	}
    }

    Thanks for all of the help!

  • Great 🙂

    Altho you could change that loop to a for-loop:

    
    for($i = 0; $i > count($gallery); $i++){ //loop through the images in the gallery
    	$gallery[$i]['constrain'] = get_field('constrain', $gallery[$i]['id']);
    }
    
    
  • I realize why my previous loop didn’t work too 🙂

    When doing the foreach loop the $image become a detached variable of the value in the array.. so adding $image[‘constrain’] does not affect the original array..

  • Awesome, thanks for explaining that. And I’ll switch that over to the for loop. It seems a lot cleaner.

    Thanks again for all of your help!

  • Np Kevin, glad to be of help 🙂

  • I tried plugging in the for loop and it seemed to stop working. It looked like this:

    				//Fetch the gallery for the selected post
    				$gallery = get_field('gallery', $post_id);
    				if($gallery){
    					//loop through the images in the gallery
    					for($i = 0; $i > count($gallery); $i++){ 
    						$gallery[$i]['constrain'] = get_field('constrain', $gallery[$i]['id']);
    					}
    				}

    I have a working model, but since you suggested it I wanted to try it.

  • Oh well.. It’s probably not counting $gallery because it’s not in the format that I had pictured in my head. Nothing to worry about since you have a working loop in any case 🙂

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

The topic ‘Custom Fields in Gallery Array’ is closed to new replies.