Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/298.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 在Wordpress'中回显随机数;获取\u模板\u零件_Php_Wordpress_Random_Echo - Fatal编程技术网

Php 在Wordpress'中回显随机数;获取\u模板\u零件

Php 在Wordpress'中回显随机数;获取\u模板\u零件,php,wordpress,random,echo,Php,Wordpress,Random,Echo,在我的Wordpress主题中,我有一个partials文件夹,其中包含10个以数字后缀命名的文件,我想使用get\u template\u part从这10个文件中随机选择一个文件。我认为最简单的方法是用数字差来命名文件,并在1到10之间重复一个随机数,该随机数将添加到get\u template\u part中列出的文件名末尾 目前我有以下内容,尽管我意识到你不能在PHP中运行PHP。它们能与这种逻辑结合起来吗 有两件事: 您需要从get\u template\u part()中删除-(连字

在我的Wordpress主题中,我有一个partials文件夹,其中包含10个以数字后缀命名的文件,我想使用
get\u template\u part
从这10个文件中随机选择一个文件。我认为最简单的方法是用数字差来命名文件,并在1到10之间重复一个随机数,该随机数将添加到
get\u template\u part
中列出的文件名末尾

目前我有以下内容,尽管我意识到你不能在PHP中运行PHP。它们能与这种逻辑结合起来吗


有两件事:

  • 您需要从
    get\u template\u part()
    中删除
    -
    (连字符),因为WP添加了该字符
  • 您可以通过变量传递随机数
  • 
    
    您当然可以在PHP中运行PHP。从
    模板-
    中删除连字符<代码>获取模板部分添加了这一点<代码>谢谢您的回复<代码>抛出一个错误:“解析错误:语法错误,意外的“echo”(T_echo),应为“')in…”,然后将变量设置为随机数<代码>是的!你明白了。我会创造一个答案,因为这很有价值
    template-1.php
    template-2.php
    template-3.php
    [...]
    template-10.php
    
    <?php
        // Assign the rand number to a variable 
        $number = rand( 1, 10 ); 
        // use the variable in the template part
        get_template_part('partials/template',  $number ); 
    ?>