Conditional brackets – either/or possible?

Home Forums Product Support Forums Ajax Search Pro for WordPress Support Conditional brackets – either/or possible?

This topic contains 5 replies, has 2 voices, and was last updated by Ernest Marcinko Ernest Marcinko 1 year, 1 month ago.

Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #42314
    koltast
    koltast
    Participant

    Hello!
    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!

    #42329
    Ernest Marcinko
    Ernest Marcinko
    Keymaster

    Hi,

    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;
    }
    Best,
    Ernest Marcinko

    If you like my products, don't forget to rate them on codecanyon :)


    #42338
    koltast
    koltast
    Participant

    Wow, thanks a lot for your effort!

    I tried your code, but here’s what it does for me:

    If $field1 has 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!

    #42350
    Ernest Marcinko
    Ernest Marcinko
    Keymaster

    Hi,

    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;
    }
    Best,
    Ernest Marcinko

    If you like my products, don't forget to rate them on codecanyon :)


    #42399
    koltast
    koltast
    Participant

    Wow, 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!
    🙂

    • This reply was modified 1 year, 1 month ago by koltast .
    #42401
    Ernest Marcinko
    Ernest Marcinko
    Keymaster
    You cannot access this content. Best,
    Ernest Marcinko

    If you like my products, don't forget to rate them on codecanyon :)


Viewing 6 posts - 1 through 6 (of 6 total)

You must be logged in to reply to this topic.