Skip Navigation

[Resolved] Creating excerpt from a custom field

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 13 replies, has 2 voices.

Last updated by Minesh 1 year, 10 months ago.

Assisted by: Minesh.

Author
Posts
#2622961

Hi,
I want to create an excerpt of a custom field. The shortcode is [shorten_string string="[types field='veranstaltung-text' output='raw']" length='50'], but I want to use it in php, like this:

echo do_shortcode ('[shorten_string string="[types field='veranstaltung-text' output='raw']" length='50']');

Unfortunately this does not work! Do you have a solution for this?

Best regards
Uwe

#2622965

Minesh
Supporter

Languages: English (English )

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

Hello. Thank you for contacting the Toolset support.

Can you please tell me where exactly on what PHP template you have added the code?

Also what code yo used for custom shortcode shorten_string ?

Can you please share all those details and let me review that and I will get back to you.

#2623003

Hi Minesh,
in functions.php of my theme I have:

// Add Shortcode for toolset field
function shorten_string( $atts ) {
$atts = shortcode_atts(
array(
'length' => '',
'string' => '',
),
$atts
);
return substr($atts['string'], 0, $atts['length'])."...";
}
add_shortcode( 'shorten_string', 'shorten_string' );

I want to display the excerpt of the field "veranstaltung-text" in 2 places:
1. In an override of the plugin "Search & Filter" in "themes/nachtfrequenz/search-filter/results.php"
2. In "themes/nachtfrequenz/single.php"

I think it has something to do with the nested inverted commas and brackets of
do_shortcode ('[shorten_string string="[types field='veranstaltung-text' output='raw']" length='50']')

Thanks
Uwe

#2623125

Minesh
Supporter

Languages: English (English )

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

The code you shared seems to be ok.

The issue seem to me here you are passing the Types shortcode: [types field='veranstaltung-text' output='raw']

To assign the "veranstaltung-text" custom field value but I would like to know here from what post ID you want to get the value?

#2623179
Screenshot 2023-07-12 100815.png

Hi Minesh,
i am not sure if i have understood your question correctly... The toolset-field "veranstaltung-text" (wysiwyg) is associated to all posts.

When I edit the code in PhpStorm I get several errors - see screenshot. Undefined constant 'veranstaltung' + Undefined constant 'text' + Undefined constant 'raw' + Expected: semicolon etc.

Best regards
Uwe

#2623191

Minesh
Supporter

Languages: English (English )

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

What if you try to replace the do_shortcode line as given under:

echo do_shortcode ("[shorten_string string='[types field='veranstaltung-text' output='raw']' length='50']");

does that helps?

#2623233

This gives the following PHP-error:
Fatal error: Uncaught TypeError: substr(): Argument #3 ($length) must be of type ?int, string given in wp-content/themes/nachtfrequenz/functions.php:360 Stack trace: #0 /wp-content/themes/nachtfrequenz/functions.php(360): substr() #1 /wp-includes/shortcodes.php(355): shorten_string() #2 [internal function]: do_shortcode_tag() #3 /wp-includes/shortcodes.php(227): preg_replace_callback() #4 /wp-content/themes/nachtfrequenz/search-filter/results.php(84): do_shortcode() #5 /wp-content/plugins/search-filter-pro/public/includes/class-search-filter-query.php(966): include('/homepages/45/d...') #6 /wp-content/plugins/search-filter-pro/public/includes/class-search-filter-display-results.php(47): Search_Filter_Query->the_results() #7 /wp-content/plugins/search-filter-pro/public/includes/class-search-filter-display-shortcode.php(790): Search_Filter_Display_Results->output_results() #8 /wp-includes/shortcodes.php(355): Search_Filter_Display_Shortcode->display_shortcode() #9 [internal function]: do_shortcode_tag() #10 /wp-includes/shortcodes.php(227): preg_replace_callback() #11 /wp-content/themes/nachtfrequenz/programm.php(25): do_shortcode() #12 /wp-includes/template-loader.php(106): include('/homepages/45/d...') #13 /wp-blog-header.php(19): require_once('/homepages/45/d...') #14 /index.php(17): require('/homepages/45/d...') #15 {main} thrown in /wp-content/themes/nachtfrequenz/functions.php on line 360

Line 360 in functions.php is:
return substr($atts['string'], 0, $atts['length'])."...";

best regards
Uwe

#2623237

Minesh
Supporter

Languages: English (English )

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

Here is the shortcode that you should use:

function shorten_string( $atts ) {
$atts = shortcode_atts(
array(
'length' => 10,
'string' => '',
),
$atts
);
return substr($atts['string'], 0, $atts['length'])."...";
}
add_shortcode( 'shorten_string', 'shorten_string' );

And you can call the above shortcode as given under:

	$my_string = types_render_field('veranstaltung-text',array('output'=>'raw'));
echo do_shortcode("[shorten_string string='".$my_string ."' length=50]");

Can you please confirm it works as expected now.

#2623251

Hi Minesh,
that works! You have saved my day! Thanks!

best regards
Uwe

#2623261

Minesh
Supporter

Languages: English (English )

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

Glad to know that solution I shared help you to resolve your issue.

You are welcome to mark resolve this ticket.

#2623551

Hi Minesh,
I want to mark this ticket as resolved, but this doesn't work! Can't submit my entries...

Best regards
Uwe

#2623555

Minesh
Supporter

Languages: English (English )

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

Are you already loggedin?

You should maybe logout and login again and then try to mark resolve this ticket with some feedback.

#2623561

No - this doesn't help! Clicking on Submit doesn't submit anything...

#2623563

Minesh
Supporter

Languages: English (English )

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

Can you try different browser and try to mark resolve the ticket, if that does not help then I will mark resolve the ticket.