[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.
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.
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;
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?
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':
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":
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.
- 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.