编写一个独特的php函数在我的网站上使用

时间:2014-11-08 作者:user2882154

我在wordpress网站上使用自定义php代码翻译日期格式,使用get\\u the\\u time。我必须在我的网站上的不同空间使用此代码,有时在一个页面上不止一次。

以下是我的代码,我将在需要时添加到自定义页面模板中:

<?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);
?>
我想知道的是,我怎样才能只声明一次这段代码,也许是在我的函数中。php,然后在需要时添加以下代码:<?php echo ($date_espagnole);?>

以下是我在函数中尝试的内容。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;
}
然后在需要时在我的自定义页面中:<?php date_translation();?>

但它不起作用。有可能做到这一点吗?如果是,我做错了什么?

非常感谢您的帮助,

1 个回复
SO网友:Jimtrim

您正在函数中返回翻译,而不是打印它。使用<?php echo date_translation(); ?> 应该解决问题。

(假定functions.php是在您使用函数时启动的)

编辑:为了遵循Wordpress惯例,您应该调用函数get_date_translation(). 如果希望函数返回结果,则应调用它the_date_translation()

结束

相关推荐

如何使用php重命名图像附件文件名

我有上传的图像ID,我需要用php重命名它。(因为我必须这样做,所以很难解释原因,我认为这些原因对于本次讨论甚至都没有必要)我有4个问题要解决:重命名主file.jpg 以及相关file-WIDTH-HEIGHT.jpg 文件-只需使用rename php function</将新文件名重新分配给附件,以便WordPress知道新文件名(我可以更新数据库中附件的“guid”列吗?)管理相关文件(文件宽度高度.jpg)。主文件和相关文件之间的关系存储在哪里</更新POST/widget/ecc上