This is a continuation from this thread, where Waqar offered great help: https://toolset.com/forums/topic/get-the-conditional-to-check-if-the-post-body-contains-a-string/
The idea was to scan post text for words such as 'Abstract:', 'Publisher's note:', etc. - if they don't appear, the conditional display will add them, but if they do appear, the conditional display won't add them.
I want to use the same solution in different views. In one of them, the posts displayed are just posts, and it all works fine. Thank you Waqar 🙂 Here is an example: hidden link entry: Midgley, Mary. Individualism and the Concept of Gaia. This is the code I am using:
[wpv-conditional if="( '[body_text_contains needle='Abstract:']' eq '0' ) AND ( '[body_text_contains needle='Note:']' eq '0' ) AND ( '[body_text_contains needle='note:']' eq '0' ) AND ( '[body_text_contains needle='Matter:']' eq '0' ) AND ( '[body_text_contains needle='matter:']' eq '0' ) AND ( '[body_text_contains needle='Syllabi']' eq '0' ) AND ( '[body_text_contains needle='Introduction:']' eq '0' ) AND ( '[body_text_contains needle='Content:']' eq '0' ) AND ( '[body_text_contains needle='Summary:']' eq '0' ) AND ( '[body_text_contains needle='Conclusion:']' eq '0' )"]
[wpv-conditional if="( $(wpcf-medium) eq '1' )"]<strong>Publisher's Note: </strong>[/wpv-conditional]
[wpv-conditional if="( $(wpcf-medium) ne '1' )"]<strong>Abstract: </strong>[/wpv-conditional]
[/wpv-conditional]
[wpv-post-body view_template="None" suppress_filters="true" output="raw"]
In the other view, the posts are part of a relationship, so what is displayed is an intermediary post that draws data from the child post. Here is an example: hidden link entry: Midgley, Mary. Individualism and the Concept of Gaia. Unfortunately, here the code is not working and I get the word 'Abstract:' twice - once because it's part of the post, the second time added by the content template. This is the code I am using:
[wpv-conditional if="( '[body_text_contains item='@blueprint-text.child' needle='Abstract:']' eq '0' ) AND ( '[body_text_contains item='@blueprint-text.child' needle='Note:']' eq '0' ) AND ( '[body_text_contains item='@blueprint-text.child' needle='note:']' eq '0' ) AND ( '[body_text_contains item='@blueprint-text.child' needle='Matter:']' eq '0' ) AND ( '[body_text_contains item='@blueprint-text.child' needle='matter:']' eq '0' ) AND ( '[body_text_contains item='@blueprint-text.child' needle='Syllabi']' eq '0' ) AND ( '[body_text_contains item='@blueprint-text.child' needle='Introduction:']' eq '0' ) AND ( '[body_text_contains item='@blueprint-text.child' needle='Content:']' eq '0' ) AND ( '[body_text_contains item='@blueprint-text.child' needle='Summary:']' eq '0' ) AND ( '[body_text_contains item='@blueprint-text.child' needle='Conclusion:']' eq '0' )"]
[wpv-conditional if="( '[wpv-post-field name='wpcf-medium' item='@blueprint-text.child']' eq '1' )"]<strong>Publisher's Note: </strong>[/wpv-conditional]
[wpv-conditional if="( '[wpv-post-field name='wpcf-medium' item='@blueprint-text.child']' ne '1' )"]<strong>Abstract: </strong>[/wpv-conditional]
[/wpv-conditional]
[wpv-post-body view_template="None" suppress_filters="true" output="raw" item="@blueprint-text.child"]
I worry it's some simple syntax error somewhere, but I just can't see it. Am I missing something?
Hi,
The custom code example shared in the previous ticket ( ref: https://toolset.com/forums/topic/get-the-conditional-to-check-if-the-post-body-contains-a-string/#post-2212611 ) only supports checking the post body/content from the current post. It doesn't support the use of the "item" attribute to check the posy body/content from some other post.
To make it support the 'item' attribute too, you'll need to update the custom shortcode slightly:
add_shortcode('body_text_contains', 'body_text_contains_func');
function body_text_contains_func($atts) {
$a = shortcode_atts( array(
'needle' => '',
'item' => '',
), $atts );
if (!empty($a['item'])) {
$content = do_shortcode("[wpv-post-body view_template='None' suppress_filters='true' item='".$a['item']."']");
} else {
$content = do_shortcode("[wpv-post-body view_template='None' suppress_filters='true']");
}
$pos = strpos($content, $a['needle']);
if($pos >= 1){
return '1';
} else {
return '0';
}
}
I hope this helps and please let me know if you need any further assistance around this.
regards,
Waqar
Fantastic, this works a dream. Thank you!!!