Skip Navigation

[Resolved] Conditional Display of Content, Condition not equal but contains

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

Problem:

The issue here is that the user is using a custom shortcode and wanted to check if the value returned is not equal to a static value.

Solution:

This can be done by using the "ne" operator in our conditional shortcode.
Example
[wpv-conditional if="( '[wpv_contains haystack='testing' needle='test']' ne 'false' )"]

This support ticket is created 5 years, 12 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
- 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 -
- 13:00 – 18:00 13:00 – 18:00 13:00 – 18:00 14:00 – 18:00 13:00 – 18:00 -

Supporter timezone: America/Jamaica (GMT-05:00)

This topic contains 9 replies, has 3 voices.

Last updated by benjaminJ-3 5 years, 11 months ago.

Assisted by: Shane.

Author
Posts
#1160058

Tell us what you are trying to do?
Still struggling with the usage of the wpv_contains function. I managed to add the function to my frontend, so it will be read in the view. In debug mode I can see, that the function always delivers "false", whether the condition is met or not.

Is there any documentation that you are following?

Is there a similar example that we can see?
https://toolset.com/forums/topic/display-field-if-it-contains-a-given-string/
https://toolset.com/forums/topic/conditional-display-wpv_contains/

What is the link to your site?
still-ikam.info/sitesmap

#1160069

That's my condition:

  [wpv-conditional if="( wpv_contains('[types field='site-users' output='raw'][/types]','[wpv-current-user info='login']') )" evaluate="false" debug ="true"]

       [wpv-map-marker map_id='map-4' marker_id='marker-6' marker_title='[wpv-post-title]' marker_icon='//still-ikam.info/wp-content/plugins/toolset-maps/resources/images/markers/Buildings.png' marker_field='wpcf-address'][wpv-post-link][/wpv-map-marker] 

  [/wpv-conditional]
 

If the condition is met, a map marker is supposed to be shown. At the moment, a marker is always shown, regardless of the condition being met or net.

#1160070

Oh, and I have added the function to the frontend functions. It looks like this:


<?php
function wpv_contains($haystack, $needle)
{
  $pos = strpos($haystack, $needle);
  if($pos !== true){
    return false;
  }
}

#1160117

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Bjoern,

Lets try converting the function to as shortcode and see if it helps.

Please try this shortcode alone without adding to the conditional as yet and let me know what it returns.

// Add Shortcode
function wpv_contains( $atts ) {

	// Attributes
	$atts = shortcode_atts(
		array(
			'haystack' => '',
			'needle' => '',
		),
		$atts
	);

	
	 
	  $pos = strpos($atts['haystack'], $atts['needle']);
	  if($pos !== true){
	    return false;
	  }
	

}
add_shortcode( 'wpv_contains', 'wpv_contains' );

[wpv_contains haystack='my-val' needle='my-val']

Thanks,
Shane

#1160126

Thanks, Shane, a good idea.
The result for me looks almost the same, though. I have integrated the shortcode in my conditional like so:

    [wpv-conditional if="( '[wpv_contains haystack='[types field='site-users' output='raw'][/types]' needle='[wpv-current-user info='login']']' eq 'false' )"]
      test
[/wpv-conditional]

      [wpv-conditional if="( '[wpv_contains haystack='[types field='site-users' output='raw'][/types]' needle='[wpv-current-user info='login']']' ne 'false' )"]
      test123
[/wpv-conditional]

I get the result test123 in my complete array:
test123
test123
test123
...

#1160653

After some more testing, I believe that the term "false" is never submitted, no matter whether a condition is met or not. But I cannot seem to find out, what exactly the shortcode delivers.
This condition works:

  [wpv-conditional if="( '[wpv_contains haystack='testing' needle='test']' ne 'false' )"]
     test output[/wpv-conditional]

I am really not sure how to solve this. Normally, it seems like an easy PHP function to me. But it simply does not work.

#1160675

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Bjoern,

Would you mind allowing me to have access to this site as well as the page where the testing is being done ?

Thanks,
Shane

#1160696

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Bjoern,

Is it that you want to display something if the string being tested is not in the haystack ?

As i've fixed the logic of the code.

So if a string is in not in the haystack then your function will return nothing. If it is then it will return true which evaluates to a 1.

Please let me know if this helps.

Thanks,
Shane

#1160700

My issue is resolved now. Thank you! The problem was that the result from the function wpv_contains does not carry true or false but 0 or 1. That was the problem with my view. Thanks to Shane for helping me find a solution!

#2062257

For anyone trying to work with this strpos() will never return TRUE so the issue with this code is "$pos !== true" which will never happen. You need to use "$pos == false" for this to work as it will only return nothing for true and false for FALSE.