[Resolved] Get field for ID in shortcode

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.

This topic contains 3 replies, has 2 voices.

Last updated by Waqas 4 years, 4 months ago.

Assigned support staff: Waqas.

Author
Posts
#304491

I am trying to:
Get field values for different post ids in a loop

I visited this URL:

I expected to see:

Different images (the right image for each post)

Instead, I got:
The image of the parent article in a loop



function relatedBooks ($atts, $content="") {

    global $wpdb;
    $str  = implode(", ", $atts);
    $arr = explode(',', $str);

    foreach($arr as $bookid) {
 
      $output .= '<li>'.$bookid.'</li>';
      $output .= types_render_field( "book-cover", array( id => $bookid, "width" => "300", "height" => "200", "proportional" => "true" ) );
     
     }

	$content = $output;


	
	return "$content";



}
#304573

Waqas
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Can you please explain that how you are using your short code?

However, apparently I suspect that you are trying to define different IDs with same repeating attribute (i.e. id=xxx id=xx id=999 and so on). The latest occurrence of the same attribute, overrides the previous one.

However, if you are not doing this, but expecting $atts as an attribute name of your comma-separated list of ids (i.e. id="1,2,3,4"), please change $atts to $atts['id'] in line after global $wpdb. Please use correct attribute name (the same as you used to pass-on the ids).

#304631

Thanks, but this wasn't the problem.
The problem is that the "types_render_field" function doesn't expect a parameter "id" - you should now that 🙂

https://toolset.com/forums/topic/types_render_field-with-a-specific-post-id/



function relatedBooks ($atts, $content="") {

    global $wpdb;
	$str  = implode(", ", $atts);
    $arr = explode(',', $str);
    $id = get_the_ID();

    foreach($arr as $bookid) {
     if ($id != $bookid) {
      $output .= '<div>';
      $output .= '<a href="'.get_permalink($bookid).'">';
      //$output .= types_render_field( "book-cover", array( "post-id" => $bookid, "output" => "raw", "width" => "300", "height" => "200", "proportional" => "true" ) );
      $output .= do_shortcode('[types field="book-cover" id="'.$bookid.'" width="150" align="none" resize="proportional" output="url"]');
      $output .= '</a></div>';
     }
 }

	$content = $output;


	
	return "$content";



}

#304722

Waqas
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Thank you for your feedback.

I know about types_render_field, but actually I was trying to understand your code first. So I can offer you a better solution.

Anyhow, looks like you have fixed the issue at your own. Please feel free to open a new thread whenever you need some help.

Have a nice day.