Skip Navigation

[Resolved] Removing Comma and Text Following

This support ticket is created 2 years, 9 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
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10: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/Kolkata (GMT+05:30)

This topic contains 4 replies, has 2 voices.

Last updated by nateW 2 years, 9 months ago.

Assisted by: Minesh.

Author
Posts
#2421067

Hi, I'm looking to find a way to remove a comma from a string and all of the text after it. This is for a real estate website, and the post title is the full address (for example: 1234 Main Street, Los Angeles, CA 90001). In one of my views, I am inserting this using just the [wpv-post-title] shortcode. However, I'm looking to show this address as shortened in certain instances, so that it shows just the street address (1234 Main Street).

I've looked at this support topic about doing the opposite and adding a comma to a number (https://toolset.com/forums/topic/comma-format/) which was very helpful and I like the idea of just adding a custom shortcode and wrapping the Toolset shortcode in it for when I need this alternative formatting. For example, [wp_format_address address='[wpv-post-title]'] or something like that. Any help would be greatly appreciated!

#2421445

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

Could you please try to use the following shortcode and you can call the shortcode as: [display_short_address]

function func_display_short_address() {
  global $post;
 $res = explode(",",$post->post_title);
 return $res[0];
}
add_shortcode( 'display_short_address', 'func_display_short_address' );

You can add the above shortcode to "Custom Code" section offered by Toolset:
=> https://toolset.com/documentation/programmer-reference/adding-custom-code/using-toolset-to-add-custom-code/

#2421731

That worked beautifully, thank you so much for your help!

Just out of curiosity, what would that code look like if I was using a custom field shortcode with my address instead of the post title? For example, [types field="property-address"][/types]

#2422043

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

In that case it would be better to pass the Types shortcode as custom shortcode attribute:

You should try to use the following code:

function func_display_short_address($atts) {
 
If(isset($atts['field_value']) and !empty($atts['field_value']) ) {
 $res = explode("," , $atts['field_value']);
 return $res[0];
}

return;
}
add_shortcode( 'display_short_address', 'func_display_short_address' );

And you can call the shortcode as:

[display_short_address field_value="[wpv-post-title]"]

or

[display_short_address field_value=" [types field='property-address' output='raw'][/types]"]

I hope the above solution will help you to resolve your issue.
'

#2422457

My issue is resolved now. Thank you!