• Hi all,

    I’m trying to make a custom function in my functions.php in a Child Theme directory.

    That’s the function:

     function conserva($estado_iunc){
    	 
    	global $wpdb;
     
     switch($estado_iunc) {
    	 
    	 case EX:
    	 $iunc_desc = "Extinta";
    	 break;
    	 
    	 case EW:
    	 $iunc_desc = "Extinta na Natureza";
    	 break;
    	 
    	 case CR:
    	 $iunc_desc = "Criticamente em Perigo";
    	 break;
    	 
    	 case EN:
    	 $iunc_desc = "Em Perigo";
    	 break;
    	 
    	 case VU:
    	 $iunc_desc = "Vulnerável";
    	 break;
    	 
    	 case NT:
    	 $iunc_desc = "Quase Amea?ada";
    	 break;
    	 
    	 case LC:
    	 $iunc_desc = "Pouco Preocupante";
    	 break;
    	 
    	 case DD:
    	 $iunc_desc = "Dados Deficientes";
    	 break;
    	 
    	 case NE:
    	 $iunc_desc = "N?o Avaliada";
    	 break;
    	 
     }
     
     return $iunc_desc;
     
     }
    add_action( 'init', 'conserva', 25 );

    And the file calling the function:

    <h6>Estado de Conserva??o - <?php conserva(the_field('esp_estado_de_conserva??o')); ?></h6>

    Where

    the_field('esp_estado_de_conserva??o')

    Is an ACF SELECT Custom Field.

    It’s showing “LC”, instead of “Pouco Preocupante”.

    Any clue?

    Ty in advance

    • This topic was modified 7 years, 8 months ago by dinhuakt.
Viewing 4 replies - 1 through 4 (of 4 total)
  • Moderator Steven Stern (sterndata)

    (@sterndata)

    Volunteer Forum Moderator

    While is that function hooked into init?

    Thread Starter dinhuakt

    (@dinhuakt)

    Hi Steve I’m using :

    add_action( 'init', 'conserva', 'iunc', 1 );

    add_action( 'init', 'conserva', 'iunc', 1 );
     function conserva($iunc){
     
     switch($iunc) {
    	 
    	 case EX:
    	 $iunc = "Extinta";
    	 break;
    	 
    	 case EW:
    	 $iunc = "Extinta na Natureza";
    	 break;
    	 
    	 case CR:
    	 $iunc = "Criticamente em Perigo";
    	 break;
    	 
    	 case EN:
    	 $iunc = "Em Perigo";
    	 break;
    	 
    	 case VU:
    	 $iunc = "Vulnerável";
    	 break;
    	 
    	 case NT:
    	 $iunc = "Quase Amea?ada";
    	 break;
    	 
    	 case LC:
    	 $iunc = "Pouco Preocupante";
    	 break;
    	 
    	 case DD:
    	 $iunc = "Dados Deficientes";
    	 break;
    	 
    	 case NE:
    	 $iunc = "N?o Avaliada";
    	 break;
    	 
     }
     
     return $iunc;
     
     }
    
    Moderator Steven Stern (sterndata)

    (@sterndata)

    Volunteer Forum Moderator

    I meant “why”. Why is that function hooked into init? What is it supposed to accomplish?

    Your call to add_action() is also not correct. The third parameter is the priority of the callback, not a value to pass into the function. Your first example is correct, but your second one is not.

    Your actual example won’t work – because you don’t do anything at the end of your function. You would need to either output or echo the value for it to do anything. As it stands now, the function gets the value, and then doesn’t do anything with it.

    function conserva($iunc){
     
        switch($iunc) {
            ....
        }
    
        return $iunc;
    }
    
    <h6>Estado de Conserva??o - <?php echo conserva(the_field('esp_estado_de_conserva??o')); ?></h6>

    But, you should do some basic de-bugging to see what’s wrong. Something like this…

    <?php
        $val = the_field( 'esp_estado_de_conserva??o' );
        echo '<p>Value: "' . $val . '" - After conserva: "' . conserva( $val ) . '"</p>';
    ?>

    If that doesn’t show what you epxect, another thing to consider is that PHP may not be handling the “special” accented characters in your field name.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Custom Function’ is closed to new replies.