Skip Navigation

[Resolved] Show post excerpt if not empty, otherwise show post body

This thread is resolved. Here is a description of the problem and solution.

Problem: I would like to show the post excerpt if it is not empty, otherwise show the full post content.

Solution:
The wpv-post-excerpt shortcode utilizes WordPress's the_excerpt function to display a post excerpt. This means that you cannot expect wpv-post-excerpt to be empty if the post contains any content. Instead, you could add this custom shortcode to your child theme's functions.php file:

add_shortcode('wpv-post-real-excerpt', 'real_excerpt_shortcode');
function real_excerpt_shortcode() {
  global $post;
  return $post->post_excerpt;
}

Go to Toolset > Settings > Frontend Content and enter "wpv-post-real-excerpt" in the 3rd party shortcodes area. Then you should be able to test the excerpt like this:

[wpv-conditional if="('[wpv-post-real-excerpt]' != '')"]
[wpv-post-excerpt]
[/wpv-conditional]
[wpv-conditional if="('[wpv-post-real-excerpt]' != '')" evaluate="false"]
[wpv-post-body view_template='None']
[/wpv-conditional]

Relevant Documentation:
https://developer.wordpress.org/reference/functions/the_excerpt/
https://toolset.com/documentation/user-guides/views-shortcodes/#wpv-post-excerpt

This support ticket is created 6 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
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

This topic contains 2 replies, has 2 voices.

Last updated by kelvinL-2 6 years, 9 months ago.

Assisted by: Christian Cox.

Author
Posts
#614892

Dear Sir/Madam,

I add below code to View

[wpv-conditional if="( '[wpv-post-excerpt]' ne '' )"]
  [wpv-post-excerpt output="raw"]
[/wpv-conditional]
[wpv-conditional if="( '[wpv-post-excerpt]' eq '' )"]
  [wpv-post-body view_template='None']
[/wpv-conditional]

but not work. No matter the excerpt is empty or not, the body content doesn't show.

#615024

Hi, the wpv-post-excerpt shortcode utilizes WordPress's the_excerpt function (https://developer.wordpress.org/reference/functions/the_excerpt/) to display a post excerpt. This means that you cannot expect wpv-post-excerpt to be empty if the post contains any content. Instead, you could add this custom shortcode to your child theme's functions.php file:

add_shortcode('wpv-post-real-excerpt', 'real_excerpt_shortcode');
function real_excerpt_shortcode() {
  global $post;
  return $post->post_excerpt;
}

Go to Toolset > Settings > Frontend Content and enter "wpv-post-real-excerpt" in the 3rd party shortcodes area. Then you should be able to test the excerpt like this:

[wpv-conditional if="('[wpv-post-real-excerpt]' != '')"]
[wpv-post-excerpt]
[/wpv-conditional]
[wpv-conditional if="('[wpv-post-real-excerpt]' != '')" evaluate="false"]
[wpv-post-body view_template='None']
[/wpv-conditional]
#615085

excellent solution