0

I'm using a custom php code to translate my date format on my wordpress website, using get_the_time. I have to use this code on various spaces on my website, sometimes more than one time on a page.

here is my code I'm adding inside my custom page templates when needed :

<?php 
$date  =  get_the_time('l j F Y');
$interdit = array("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday","Sunday","January","February","March","April","May","June","July","August","September","October","November","December");
$replace   = array("Lunes", "Martes", "Miércoles", "Jueves", "Viernes", "Sábado","Domingo", "Enero","Febrero","Marzo","Abril","Mayo","Junio","Julio","Agosto","Septiembre","Octubre","Noviembre","Diciembre");

$date_espagnole = str_replace($interdit, $replace, $date);
echo ($date_espagnole);
?>

what I want to know is how can I only declare this code once, maybe in my function.php, and then just add this code when needed : <?php echo ($date_espagnole);?>

Here is what I've tried in my function.php :

function date_translation(){
$date  =  get_the_time('l j F Y');
$interdit = array("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday","Sunday","January","February","March","April","May","June","July","August","September","October","November","December");
$replace   = array("Lunes", "Martes", "Miércoles", "Jueves", "Viernes", "Sábado","Domingo", "Enero","Febrero","Marzo","Abril","Mayo","Junio","Julio","Agosto","Septiembre","Octubre","Noviembre","Diciembre");

$date_espagnole = str_replace($interdit, $replace, $date);
return $date_espagnole;
}

and then in my custom pages when needed : <?php date_translation();?>

but it's not working. Is it possible to do this ? and if yes, what am I doing wrong ?

thanks a lot for your help,

1

You are returning the translation in your function, not printing it. Using <?php echo date_translation(); ?> should solve the problem.

(given that functions.php is initiated at the time you use the function)

Edit: In order to follow Wordpress convention, you should call the function get_date_translation(). If you want the function to echo the result instead, you should call it the_date_translation()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.