Skip Navigation

[Resolved] Checking if has excerpt

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
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9: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/Karachi (GMT+05:00)

This topic contains 5 replies, has 3 voices.

Last updated by Waqar 1 year, 1 month ago.

Assisted by: Waqar.

Author
Posts
#2663935

Hi there, I'm migrating a site which has a lot of manual excerpts, so they should be shown in Views, but if there isn't a manual excerpt, the body should be shown.
Following this guide,
https://toolset.com/documentation/programmer-reference/views/using-custom-functions-in-conditions/#checking-for-empty-post-content

This snippet in functions.php

```
/*Check if has excerpt*/

function wpv_conditional_post_has_excerpt($type, $object) {
$return = 0;
if ( $type == 'posts' ) {
if ( empty( $object->post_excerpt ) ) {
// return 0 when post's excerpt is empty
$return = 0;
} else {
// return 1 when post's excerpt is not empty
$return = 1;
}
}
return $return;
}

```
All this below shortcode showing posts in loop returns nothing
```
[wpv-conditional if="( wpv_conditional_post_has_excerpt() eq '1' )"]
<p> I have an excerpt </p>
[wpv-post-excerpt output="raw"]
[/wpv-conditional]
[wpv-conditional if="( wpv_conditional_post_has_excerpt() eq '0' )"]
<p>I have no excerpt</p>
[/wpv-conditional]
```
Please let me know about the obvious things i'm missing!
Oh here's a debug message if that helps?

```
####################
wpv-conditional attributes
####################
Array
(
[debug] => true
[if] => ( wpv_conditional_post_has_excerpt() = '1' )
)

####################
Debug information
####################
--------------------
Original expression: ( wpv_conditional_post_has_excerpt() = '1' )
--------------------
After matching 1 numeric strings into real numbers: ( wpv_conditional_post_has_excerpt() = 1 )
Matched '1' to 1
```

#2664245

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Hi there

Only some of your content has manual excerpts, and you are adopting a solution to output the manual excerpt if it exists but the post content if it doesn't, is that right?

I'm not sure you need such an elaborate solution.

The wpv-post-excerpt shortcode already outputs the manual excerpt if it exists and an automatic excerpt if it doesn't.

You can set the word length of the excerpt to an arbitrarily high value if you want all of the content to be output.

Is there a reason that isn't a solution?

#2664389

Oh, you're quite right!
[wpv-post-excerpt] should do this job already,
but I've broken it by replacing wp_trim_excerpt()
in order to include some links and other tags in the excerpt, as that's in the content that needs to be migrated.

Would you be able to suggest a better way to allow <a><br> in the manual excerpts without breaking [wpv-post-excerpt?

Thanks for suggestions

/*Allow links, bold, italics in excerpts */

function improved_trim_excerpt($text) {
$raw_excerpt = $text;
if ( '' == $text ) {
$text = get_the_content('');
$text = strip_shortcodes( $text );
$text = apply_filters('the_content', $text);
$text = str_replace('\]\]\>', ']]>', $text);
$text = strip_tags($text, '<strong><em><a><br>');
$excerpt_length = apply_filters('excerpt_length', 50);
$newexcerpt_more = apply_filters('excerpt_more', 'new_excerpt_more');
$words = preg_split("/[\n\r\t ]+/", $text, $excerpt_length + 1, PREG_SPLIT_NO_EMPTY);
if ( count($words) > $excerpt_length ) {
    array_pop($words);
    $text = implode(' ', $words);
    $text = $text . $newexcerpt_more;
    $text = force_balance_tags( $text );
} else {
    $text = implode(' ', $words);
    $text = force_balance_tags( $text );
}
}
return $text;
}
remove_filter('get_the_excerpt', 'wp_trim_excerpt');
add_filter('get_the_excerpt', 'improved_trim_excerpt');

#2664391

oops

#2664823

Sorry is it better to start a new thread/ticket for this issue?

#2664859

Hi,

Thank you for sharing this update.

Based on what you've shared, I'll recommend not to use the 'get_the_excerpt' filter, as it affects the excerpts functionality globally, including for the 'wpv-post-excerpt' shortcode.

Instead, you can register a custom shortcode, that can be used specifically wherever you'd like to display the excerpts/trimmed content with the selected HTML tags allowed:
https://codex.wordpress.org/Shortcode_API

The logic of the output of this custom shortcode would be very similar to the 'improved_trim_excerpt' function that you already have. But, it just won't be filtering the excerpts, in general through the 'get_the_excerpt' filter.

I hope this helps and please let me know if you need further assistance.

regards,
Waqar