Support

Account

Home Forums Add-ons Gallery Field Custom Fields in Gallery Array Reply To: Custom Fields in Gallery Array

  • 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.