Skip Navigation

[Resolved] Trouble bringing in the ALT text on an image

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

Problem:

Use Types shortcode in custom PHP function.

Solution:

It is a custom PHP codes problem, see the solution here:

https://toolset.com/forums/topic/trouble-bringing-in-the-alt-text-on-an-image/#post-2205585

Relevant Documentation:

This support ticket is created 3 years, 6 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.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Hong_Kong (GMT+08:00)

This topic contains 3 replies, has 2 voices.

Last updated by julesW 3 years, 6 months ago.

Assisted by: Luo Yang.

Author
Posts
#2203245
toolset_alt-text.png

Hello

I'm trying to get bring in the ALT tag from an image. I'm using https://toolset.com/documentation/customizing-sites-using-php/functions/ and https://toolset.com/forums/topic/types_render_field-with-a-specific-post-id/ as a reference.

I've attached an image for you to see the results on the front end.

The issue - alt="%%ALT%%" does not bring in the alt text.

Specific line of code:
$shortcode = sprintf('[types field="litter-puppy-image" id="%1$s" size="medium" alt="%%ALT%%" output="html"]', $puppy->ID);

Below is the full code bit I"m working with
------------------

if(is_singular('litters')){
$litter_id = get_the_ID();
$puppies_in_litter_output = '';
$puppies_in_litter = toolset_get_related_posts( $litter_id, 'litter-puppies', array( 'query_by_role' => 'parent', 'return' => 'post_object' ) );
foreach ($puppies_in_litter as $puppy){
$puppy_full_name = get_post_meta($puppy->ID, 'wpcf-litter-puppy-name', true);
$puppy_nick_name = get_post_meta($puppy->ID, 'wpcf-litter-puppy-nickname', true);
$puppy_description = get_post_meta($puppy->ID, 'wpcf-litter-puppy-description', true);
$puppy_image_slug = get_post_meta($puppy->ID, 'wpcf-litter-puppy-image', true);
$shortcode = sprintf('[types field="litter-puppy-image" id="%1$s" size="medium" alt="%%ALT%%" output="html"]', $puppy->ID);
$thumbnail = do_shortcode($shortcode);
$puppies_in_litter_output .= sprintf('

  • Full Name: %1$s<div>Nickname: %2$s</div><div>Description: %3$s</div><div>Image: %4$s</div>
  • ', $puppy_full_name, $puppy_nick_name, $puppy_description, $thumbnail);
    }
    if($puppies_in_litter_output!==''){
    echo sprintf('<div class="et_pb_module et_pb_text et_pb_text_align_left et_pb_bg_layout_light ul-column-count-2"><div class="et_pb_text_inner">

      %1$s

    </div></div>', $puppies_in_litter_output);
    }
    if($puppies_in_litter_output ==''){
    echo sprintf('<div class="et_pb_module et_pb_text et_pb_text_align_left et_pb_bg_layout_light"><div class="et_pb_text_inner"><p class="text-center">Photos and Profiles Coming Soon!</p></div></div>');
    }
    }

    --------

    Is this something you can assist with?
    Thank you!
    Jules

    #2204633

    Hello,

    Since it is a custom codes problem, please provide a test site with the same problem, fill below private message box with login details and FTP access, also point out the problem page URL, and where I can edit your custom PHP codes, I need a live website to test and debug this issue

    #2205585

    Thanks for the details, since you are using PHP function sprintf to generate the shortcode, it will remove % characters, and conduct the problem, I have done below modifications in your website:
    Edit the "Puppies in Litter" Module, replace line 11, from:

    $shortcode = sprintf('[types field="litter-puppy-image" id="%1$s" size="medium" alt="%%ALT%%" output="html"]', $puppy->ID);
    

    To:

    		$shortcode = str_replace('%1$s', $puppy->ID, '[types field="litter-puppy-image" item="%1$s" size="medium" alt="%%ALT%%" output="html"]');
    

    Please test again, check if it is fixed, thanks

    More help:
    hidden link

    #2206433

    Thank you, Luo Yang!

    That did the trick. Thanks for the resource link as well.

    Cheers!
    Jules