Home › Forums › Product Support Forums › Ajax Search Pro for WordPress Support › Conditional brackets – either/or possible?
- This topic has 5 replies, 2 voices, and was last updated 3 years ago by
Ernest Marcinko.
-
AuthorPosts
-
April 27, 2023 at 5:32 am #42314
koltast
ParticipantHello!
Is it possible to have conditional brackets like this:[ {A} || {B} ]If A has a value then show A, but not B.
If A does not have a value then show B.Thank you for your help!
April 27, 2023 at 1:58 pm #42329Ernest Marcinko
KeymasterHi,
I’m afraid that is not possible via the bracket syntax.
You can however use the asp_results hook to modify the results fields with a bit of custom code. With that you can basically do anything you want to.
Something like this maybe:
add_filter( 'asp_results', 'asp_my_custom_field_code', 10, 1 ); function asp_my_custom_field_code( $results ) { // Field names $field1_key = 'field_1'; $field2_key = 'field_2'; foreach ($results as $k=>&$r) { // ACF support if ( function_exists('get_field') ) { $field1 = get_field( $field1_key, $r->id, true ); $field2 = get_field( $field2_key, $r->id, true ); } else { $field1 = get_post_meta( $r->id, $field1_key, true ); $field2 = get_post_meta( $r->id, $field2_key, true ); } // Change only, if the meta is specified if ( empty($field1) ) { if ( !empty($field2) ) { $r->content = $field2; } } else { $r->content = $field1; } } return $results; }April 28, 2023 at 10:57 am #42338koltast
ParticipantWow, thanks a lot for your effort!
I tried your code, but here’s what it does for me:
If
$field1has a value, the Advanced Description Field (where I inserted the{asp_my_custom_field_code}) only shows that value, but nothing else any more. The rest of the Advanced Description Field is deleted. If it doesn’t have a value, it returns nothing and shows the rest of the field as usual.Do you have a quick idea how to make it that the field still shows the rest, but inserts the value of the function?
I don’t want to bother yo too much with this 🙂Thanks a lot and have a great day!
May 1, 2023 at 1:39 pm #42350Ernest Marcinko
KeymasterHi,
Well, the code only handles a simple case where the whole content field is replaced with the custom field values – regarless what the advanced title/content fields hold.
To make a replacement for the content field this has to be modified a bit. First of all for the advanced title/content field we will have to add a new “pseudo” variable, which is not regognized, and the custom code will deal with it and replace it with the output of the field logic, for that let’s use the made up variable name
$$$field_values$$$.
Wherever you will place this variable in the advanced content field it is going to be replaced by the custom code below. So you can do something like:{descriptionfield} $$$field_values$$$Then the custom code (don’t forget to replace the field1_key and field12_key variable values with the field keys):
add_filter( 'asp_results', 'asp_my_custom_field_code', 10, 1 ); function asp_my_custom_field_code( $results ) { // Field names $field1_key = 'field_1'; $field2_key = 'field_2'; // --- DO NOT CHANGE ANYTHING BELOW --- foreach ($results as $k=>&$r) { $replacement = ''; // ACF support if ( function_exists('get_field') ) { $field1 = get_field( $field1_key, $r->id, true ); $field2 = get_field( $field2_key, $r->id, true ); } else { $field1 = get_post_meta( $r->id, $field1_key, true ); $field2 = get_post_meta( $r->id, $field2_key, true ); } // Change only, if the meta is specified if ( empty($field1) ) { if ( !empty($field2) ) { $replacement = $field2; } } else { $replacement = $field1; } $r->content = str_replace('$$$field_values$$$', $replacement, $r->content); } return $results; }May 9, 2023 at 12:33 pm #42399koltast
ParticipantWow, you’re so great!
Thanks a lot, this code works for me!I use it to show the author of a post. And if the client wants a different name to show as author he can enter it in a custom field, then that name is shown instead.
Thanks again and have a great day!
🙂May 9, 2023 at 12:42 pm #42401Ernest Marcinko
KeymasterYou cannot access this content.
-
AuthorPosts
- You must be logged in to reply to this topic.