Skip Navigation

[Resolved] How to display a specific item from a field with multiple instances in e2PDF shortcode

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

Problem: I am using a shortcode from the e2PDF plugin that displays a Types custom field value. However, my field allows multiple instances and I would like to display one specific index of that field. How can I customize the shortcode to work with Types repeating field values?

Solution: Response from e2PDF support:
As for now you can try to hook default output with adding filter to your theme functions.php:

add_filter('e2pdf_model_shortcode_e2pdf_wp_response', 'e2pdf_toolset_types_repeater', 10, 3);
function e2pdf_toolset_types_repeater($response, $atts, $value) {
    if (isset($atts['key']) && $atts['key'] == 'wpcf-listing-photo' && isset($atts['index'])) {
         
        $id = isset($atts['id']) ? $atts['id'] : false; // Post ID
        $index = $atts['index']; // Index attribute
        $field = str_replace('wpcf-', '', $atts['key']); // Converting field key to slug needed for Tooset Types
         
        if ($id) {
            //php function to render value
            $response = types_render_field($field, array("post_id" => $id, "size" => "thumbnail", "index" => $index));
        } else {
            $response = '';
        }
    }
    return $response;
}

After adding function you must be able to use shortcode as follows:

[e2pdf-wp key="wpcf-listing-photo" meta="true" index="0"]
[e2pdf-wp key="wpcf-listing-photo" meta="true" index="1"]

and so on...

For correct usage "types_render_field" function must return "empty" value in case index not exists or url for image if image found, so maybe it will be need to add some parameters to "types_render_field". Unfortunately we can't test it on our side as we do not have yet access to Toolset Types plugin.

Relevant Documentation:
https://toolset.com/documentation/customizing-sites-using-php/functions/

This support ticket is created 4 years, 1 month 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.

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 3 replies, has 2 voices.

Last updated by Christian Cox 4 years, 1 month ago.

Assisted by: Christian Cox.

Author
Posts
#1855839

I'm using a plugin to create a downloadable PDF of a custom post type.

It's really easy to map each PDF entry to Toolset custom fields. However, the one sticking point is a Types image field that has multiple instances.

I use the following shortcode to grab the value from the field. But on the front end, this only shows from first instance from the multiple instance image field.

(I understand why this happens; I assume the mapping only finds the first instance in the database.)

[e2pdf-wp key="wpcf-listing-photo" meta="true"]

Based on this shortcode, would there be a way to target another instance? I tried adding index=1, but don't know if I added this to the correct place of if that would even be possible.

Any suggestions would be helpful. Thanks!

#1855851
Screen Shot 2020-11-24 at 2.53.22 PM.png

Hello, I'm not really the best source of information about the features provided by a 3rd-party plugin's shortcodes. You would need to contact their support team for more information about retrieving the value of a specific index of a repeating postmeta field. It may or may not be possible in their shortcode, depending on how it is programmed to function. Their support team would be able to tell you if their shortcode supports additional attributes to query a specific index in a repeating field value. Or if their documentation is available online, you may be able to determine this yourself.

I can give you more information about how repeating image fields from Types are stored in the database, hopefully that's helpful for a discussion with their support team. Repeating image fields are stored as separate postmeta values using the same meta key. For example, if there are two images in your repeating image field, you will have two entries in postmeta for the corresponding post ID using the same meta key "wpcf-listing-photo". In my screenshot here, you can see two entries in postmeta for the field slug wpcf-book-rept-img-1, both belonging to the post with ID 722.

Let me know if you need additional information from our side to help facilitate that conversation with your 3rd-party plugin support team. I'm glad to help.

#1859429

Thanks Christian, I passed on your technical explanation to support at e2PDF and they were able to figure out a solution. Basically just required some code in functions.php to make their shortcode recognize the index number for each instance fo the field.

Thanks for your assistance.

For anyone who stumbles upon this thread here, check out the solution in this support thread at e2PDF:

hidden link

#1860445

Thank you so much for sharing the custom solution provided by the e2PDF team. I'm adding more information here for future reference.

Response from e2PDF -

As for now you can try to hook default output with adding filter to your theme functions.php:

add_filter('e2pdf_model_shortcode_e2pdf_wp_response', 'e2pdf_toolset_types_repeater', 10, 3);
function e2pdf_toolset_types_repeater($response, $atts, $value) {
    if (isset($atts['key']) && $atts['key'] == 'wpcf-listing-photo' && isset($atts['index'])) {
        
        $id = isset($atts['id']) ? $atts['id'] : false; // Post ID
        $index = $atts['index']; // Index attribute
        $field = str_replace('wpcf-', '', $atts['key']); // Converting field key to slug needed for Tooset Types
        
        if ($id) {
            //php function to render value
            $response = types_render_field($field, array("post_id" => $id, "size" => "thumbnail", "index" => $index));
        } else {
            $response = '';
        }
    }
    return $response;
}

After adding function you must be able to use shortcode as follows:

[e2pdf-wp key="wpcf-listing-photo" meta="true" index="0"]
[e2pdf-wp key="wpcf-listing-photo" meta="true" index="1"]

and so on...

For correct usage "types_render_field" function must return "empty" value in case index not exists or url for image if image found, so maybe it will be need to add some parameters to "types_render_field". Unfortunately we can't test it on our side as we do not have yet access to Toolset Types plugin.