Aloha - this is a follow-up inquiry to thread previously resolved:
https://toolset.com/forums/topic/captions-display/
Tell us what you are trying to do?
Troubleshoot a pair of display issues associated with a custom solution you provided for me a few months ago.
1) Broken captions
2) loading FULL images ... would like to dumb down to MEDIUM
The issue didn't appear at the time you helped me - I just found it yesterday while preparing some training materials.
---
To summarize, I'm trying to display images/captions in the right sidebar of a POST template.
For the most part - it's working GREAT! 🙂
The is the shortcode used on the template: [repeating-media-field field="wpcf-pimages"]
Here is the custom code:
<?php
/**
* New custom code snippet (replace this with snippet description).
*/
toolset_snippet_security_check() or die( 'Direct access is not allowed' );
// Put the code of your snippet below this comment.
add_shortcode('media-field', function ($atts = [], $content = null) {
// provide default
$atts = shortcode_atts(
array(
'output' => 'title',
),
$atts
);
$output = '';
if (isset($content)) {
$url = do_shortcode( $content );
// get the ID of the media post for the URL contained in content
$media_id = attachment_url_to_postid($url);
$media = get_post($media_id);
switch ($atts['output']) {
case 'title':
$output = $media->post_title;
break;
case 'caption':
$output = $media->post_excerpt;
break;
case 'description':
$output = $media->post_content;
break;
case 'alt':
$output = get_post_meta( $media_id, '_wp_attachment_image_alt', true );
break;
}
}
return $output;
});
add_shortcode('repeating-media-field', function($atts = []){
global $post;
// provide default
$atts = shortcode_atts(
array(
'field' => 'wpcf-pimages',
),
$atts
);
$output = '';
$images = get_post_meta( $post->ID, $atts['field'], false); // false to return an array of image_urls
foreach( $images as $index => $image) {
// get the image, note how we use $index
$output .= wpv_do_shortcode('[types field="pimages" index="' . $index . '" title="%%TITLE%%" alt="%%ALT%%" size="full"][/types]');
// get the image's caption
$output .= wpv_do_shortcode('[media-field output="caption"]' . $image . '[/media-field]');
}
return $output;
});
Is there any documentation that you are following?
Just what was offered in the thread above.
Is there a similar example that we can see?
Yes - hidden link
Here is the issue with images in the right sidebar (max pixel dimensions are listed on images, and under as a Caption).
1) Captions for images that were uploaded UNDER the WordPress max of 2560 pixels are working just fine (see attached image).
2) Captions for images that were uploaded OVER the WordPress max of 2560 pixels are failing - oddly, Captions are displaying the POST EXCERPT (see attached image).
Clearly, something to do with WP image auto-scaling.
I'd really like TWO things to happen.
1) Adress the broken caption/excerpt issue.
1) Can the code pull the MEDIUM image - rather than the FULL?
--- I'm wondering if this alone might fix the weird broken caption for images being scaled on import by WP.
Pulling the MEDIUM image will also be very helpful in getting my page speeds down.
I tried it myself in the code above (size="full" to size="medium") but it broke the display - so I panicked and rolled back 🙂
Sorry, I lack the confidence to do this myself.
What is the link to your site?
hidden link
Millions of thanks for your continued support -- Toolset is AWESOME.