简体   繁体   English

如何在 Wordpress 的简码中使用此 PHP 代码?

[英]How to use this PHP code in a shortcode for Wordpress?

How can I use the following code in a WordPress shortcode?如何在 WordPress 短代码中使用以下代码?

//Variables
$DesdeLetra = "a";
$HastaLetra = "z";
$DesdeNumero = 1;
$HastaNumero = 10000;

$letraAleatoria = chr(rand(ord($DesdeLetra), ord($HastaLetra)));
$numeroAleatorio = rand($DesdeNumero, $HastaNumero);

echo "<strong>La letra aleatoria generada en PHP es:</strong> ".$letraAleatoria."<br/>";
echo "<strong>El número aleatorio generado en PHP es:</strong> ".$numeroAleatorio."<br/>";

Assuming that the code works, you can try this:假设代码有效,你可以试试这个:

add_shortcode( 'yourshortcode', 'so61578297_custom_shortcode' );
function so61578297_custom_shortcode() {

  $DesdeLetra = "a";
  $HastaLetra = "z";
  $DesdeNumero = 1;
  $HastaNumero = 10000;

  $letraAleatoria = chr(rand(ord($DesdeLetra), ord($HastaLetra)));
  $numeroAleatorio = rand($DesdeNumero, $HastaNumero);

  $r = "<strong>La letra aleatoria generada en PHP es:</strong> ".$letraAleatoria."<br/>";
  $r .= "<strong>El número aleatorio generado en PHP es:</strong> ".$numeroAleatorio."<br/>";

  return $r;

}

Past this snippet in your theme's functions.php file, then you can use the shortcode in the WP editor:在主题的 functions.php 文件中通过此代码段,然后您可以在 WP 编辑器中使用短代码:

[yourshortcode]

As you are generating random letters/numbers, be sure to not have any caching active on that page.当您生成随机字母/数字时,请确保该页面上没有任何活动缓存。

Good luck!祝你好运!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM