Skip Navigation

[Resolved] Have 2 Content field in From, need one to be called in other post

This thread is resolved. Here is a description of the problem and solution.

Problem: I would like to display a gallery of images for each post, and I would like for my site users to be able to create posts with image galleries.

Solution: Use a repeating image custom field to allow your user to upload several images to place in a gallery. Use the WordPress gallery shortcode to display those images. Add the following code to functions.php:

function prefix_get_img_ids($atts) {
  global $post;
  $postid = isset($atts['post_id']) ? $atts['post_id'] : $post->ID;
  $images[] = get_post_meta($postid, 'wpcf-repeating-image-slug', true);
 
  $ids = array();
 
  global $wpdb;
 
  foreach($images as $img) {
  $query = "SELECT ID FROM {$wpdb->posts} WHERE guid='$img'";
    $id = $wpdb->get_var($query);
    if($id)
      $ids[] = $id;
  }
 
  return implode(",",$ids);
}
add_shortcode("get_image_ids", "prefix_get_img_ids");

Then use the gallery shortcode like so:

[gallery ids="[get_image_ids post_id='[wpv-post-id]']"]

Relevant Documentation: https://toolset.com/documentation/user-guides/repeating-fields/

This support ticket is created 7 years, 3 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Our next available supporter will start replying to tickets in about 2.19 hours from now. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

This topic contains 2 replies, has 2 voices.

Last updated by adriS 7 years, 3 months ago.

Assisted by: Christian Cox.

Author
Posts
#559451
Screen Shot 2017-08-14 at 10.30.49 AM.png
Screen Shot 2017-08-14 at 10.30.58 AM.png
Screen Shot 2017-08-14 at 10.35.07 AM.png

I want the client to be able to create the content for his post front end. The post must have the ablitlity to create a gallery on the post page and that gallery needs to be able to automatically be added to the Gallery page.

So the idea was to have to body content sections on front end from, one to write content and one to create the gallery. The gallry content I want to call again on the gallery page, with the past heading and thumbnails gallery.

My client is disable and see very difficult so it needs to be as simple as possible. How can I make him create a gallery front end with his post, with the abbility to seperate the gallery from the post so that it can display in Main Gallery page aswel.

#559600
Screen Shot 2017-08-14 at 11.40.33 AM.png

I think a repeating image field would be easier to use than a WYSIWYG field, where you have to click "Add Media", then navigate through the Media Library, then insert code, etc. With a repeating image field, the user clicks "Add image" then selects their image on the desktop, and it's done. It's a simpler process. See the attached screenshot. Do you agree?

More information about repeating fields here:
https://toolset.com/documentation/user-guides/repeating-fields/

To display a gallery, you can use the WordPress gallery shortcode. The only issue here is that the gallery shortcode requires a list of image IDs. Types repeating image fields contains a list of URLs. So a custom shortcode is required to retrieve a list of image IDs. Add to your functions.php file:

function prefix_get_img_ids($atts) {
  global $post;
  $postid = isset($atts['post_id']) ? $atts['post_id'] : $post->ID;
  $images[] = get_post_meta($postid, 'wpcf-repeating-image-slug', true);

  $ids = array();

  global $wpdb;

  foreach($images as $img) {
  $query = "SELECT ID FROM {$wpdb->posts} WHERE guid='$img'";
    $id = $wpdb->get_var($query);
    if($id)
      $ids[] = $id;
  }

  return implode(",",$ids);
}
add_shortcode("get_image_ids", "prefix_get_img_ids");

Then you must register 'get_image_ids' and 'wpv-post-id' in Toolset > Settings > Front-end Content > Third-party shortcode arguments.

Now you should be able to use the gallery shortcode and the get_image_ids shortcode to display a gallery in any post:

[gallery ids="[get_image_ids post_id='[wpv-post-id]']"]

Place that code in a Content Template or Layout for your posts. If you want to place this gallery on the gallery page as well, you should create a View of posts. In the Loop Output editor, place the post title and the gallery shortcode. The gallery will appear for each post.

This is a lot of instruction, I know, but once you set it up it will be easy to keep up-to-date, and your visually impaired user will have the least number of steps to follow. Let me know if you have additional questions about this.

#559661

Thank you very very much