Skip Navigation

[Resolved] Render "Display text" vs "Custom field content" in PHP

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.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

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

Supporter timezone: America/Sao_Paulo (GMT-03:00)

Tagged: 

This topic contains 6 replies, has 2 voices.

Last updated by Mateus Getulio 1 year, 1 month ago.

Assisted by: Mateus Getulio.

Author
Posts
#2653421
Screenshot 2023-10-16 at 3.37.23 PM.png

How can I choose to display the value from "Display Text" from a select input field?

If I use the following code in Views, It returns "GA":
[types field="state" id="$locations"][/types].

If I use the following code in PHP, it returns "Georgia":
$postid = get_the_ID();
$parent_location = toolset_get_related_post( $postid, array( 'locations', 'career-opportunity' ) );
echo '<span>' . get_post_meta($parent_location, 'wpcf-state', true) . '</span>';

I would like to display "GA" instead of "Georgia" in PHP.

Thanks

#2653827

Mateus Getulio
Supporter

Languages: English (English )

Timezone: America/Sao_Paulo (GMT-03:00)

Hey there,

Thank you for contacting our support.

Can you try the following code to see if it helps you achieving what you need?

$postid = get_the_ID();
$parent_location = toolset_get_related_post($postid, array('locations', 'career-opportunity'));
$state_full = get_post_meta($parent_location, 'wpcf-state', true);

// Create an associative array to map full state names to abbreviations
$state_mappings = array(
    'Alabama' => 'AL',
    'Alaska' => 'AK',
    // Add more state mappings here...
    'Georgia' => 'GA',
    'Wyoming' => 'WY'
);

// Check if the state name exists in the mappings array
if (isset($state_mappings[$state_full])) {
    $state_abbreviation = $state_mappings[$state_full];
    echo '<span>' . $state_abbreviation . '</span>';
} else {
    // If the state name is not found in the mappings array, just display the original value
    echo '<span>' . $state_full . '</span>';
}

You might need to complete the mapping with the other states and eventually change the code to better suit your needs;

Please give it a try and tell us the results.

Best,

#2653829

Thanks Mateus, but Is there a more simple solution for this? I'm looking to update several single post templates with several locations but don't want to add alot of code to each page templates.

What is the difference between "Display Text" and "Custom field content" and why does it return different results in View vs PHP?

#2653881

Mateus Getulio
Supporter

Languages: English (English )

Timezone: America/Sao_Paulo (GMT-03:00)

Hello there,

I checked it and in fact there's a much simpler way of doing it. Firstly, please check our documentation on working with the Toolset Select input with PHP: https://toolset.com/documentation/customizing-sites-using-php/functions/#select

Instead of using 'get_post_meta' you'd need to use 'types_render_field'. Then you can change the behavior of the function to either echo the display text or the field content as per your need.

For example:

If you set the parameter 'show_name' to false or omit this param, it should echo out the display text which in this case would be 'GA':

<?php echo(types_render_field("state", array('show_name' => 'true'))); ?>

Or, setting 'show_name' as true should bring the field content 'Georgia':

<?php echo(types_render_field("state", array('show_name' => 'true'))); ?>

Can you please give it a try using types_render_field instead and tell us if you achieved what you wanted?

Thank you, please let us know.

#2653915

Hi Mateus, I made several attempts to get this to work but none took effect. Can you update your code to work with related posts? I'm not sure why its not working.

As I mentioned in my original requests, this is my original working code that results in "Georgia":

$postid = get_the_ID();
$parent_location = toolset_get_related_post( $postid, array( 'locations', 'career-opportunity' ) );
echo '<span>' . get_post_meta($parent_location, 'wpcf-state', true) . '</span>';

Can you provide an updated code that will work to display "GA"?

I have tried the following but did not display any value:

echo types_render_field($parent_location, 'state', true );
echo types_render_field($parent_location, 'state', array('show_name' => 'true'));
echo (types_render_field($parent_location, "state", array('show_name' => 'true'))) ;
echo (types_render_field("state", array('show_name' => 'true')));
#2654081

Mateus Getulio
Supporter

Languages: English (English )

Timezone: America/Sao_Paulo (GMT-03:00)

Hello,

I would like to request temporary access (wp-admin and FTP) to your site to take better look at the issue. You will find the needed fields for this below the comment area when you log in to leave your next reply. The information you will enter is private which means only you and I can see and have access to it.

Our Debugging Procedures

I will be checking various settings in the backend to see if the issue can be resolved. Although I won't be making changes that affect the live site, it is still good practice to backup the site before providing us access. In the event that we do need to debug the site further, I will duplicate the site and work in a separate, local development environment to avoid affecting the live site.

Privacy and Security Policy

We have strict policies regarding privacy and access to your information. Please see:
https://toolset.com/purchase/support-policy/privacy-and-security-when-providing-debug-information-for-support/

**IMPORTANT**

- Please make a backup of site files and database before providing us access.
- If you do not see the wp-admin/FTP fields this means your post & website login details will be made PUBLIC. DO NOT post your website details unless you see the required wp-admin/FTP fields. If you do not, please ask me to enable the private box. The private box looks like this: hidden link

Please, let me know if you need any additional details. Have a nice day.

#2654249

Mateus Getulio
Supporter

Languages: English (English )

Timezone: America/Sao_Paulo (GMT-03:00)

Hey there,

I checked it and the code wasn't working initially because that template was referring to state using the function 'toolset_get_related_post'.

To fix it, I used the variable $parent_location to feed types_render_field passing it as an item in the array.

Please try the following code and tell me the results:

	echo(types_render_field("state", array('show_name' => 'false', "item" => $parent_location))); 

I tested it in the package you provided us with and it worked fine in my tests.

Thank you, please let us know.

Mateus

#2655367

Thanks Mateus, that worked!