Php Wordpress短代码不显示结果

Php Wordpress短代码不显示结果,php,wordpress,shortcode,Php,Wordpress,Shortcode,我一直在尝试为我的Wordpress主题创建自己的短代码,以使生活更轻松。不幸的是,我的代码遇到了问题 下面,Javascript部分中的代码是我在functions.php文件中放置的代码,HTML部分中的代码是我在Wordpress页面中放置的短代码,但是似乎什么都没有出现 1:JAVASCRIPT 2:HTML 功能租用设备功能($atts){ 提取(短码)附件(数组)( “img”=>“无img”, “用户”=>“无用户”, “文本”=>“无文本”, )美元(附件);; 如果($atts

我一直在尝试为我的Wordpress主题创建自己的短代码,以使生活更轻松。不幸的是,我的代码遇到了问题

下面,Javascript部分中的代码是我在functions.php文件中放置的代码,HTML部分中的代码是我在Wordpress页面中放置的短代码,但是似乎什么都没有出现

1:JAVASCRIPT

2:HTML
功能租用设备功能($atts){
提取(短码)附件(数组)(
“img”=>“无img”,
“用户”=>“无用户”,
“文本”=>“无文本”,
)美元(附件);;
如果($atts['img']=''| |$atts['user']=''| |$atts['text']='')返回;
$output=
'
“.$atts['img']”。
“.$atts['user']” “.$atts['text']” '; 返回$output; } 添加_快捷码('hire_equipment'、'hire_equipment_func')
[hire\u device img=”“user=“Test Float”text=“可在任何地方使用”]
功能租用设备功能($atts){
提取(短码)附件(数组)(
“img”=>“无img”,
“用户”=>“无用户”,
“文本”=>“无文本”,
)美元(附件);;
if($atts['img']='no-img'| |$atts['user']=='no-user'| |$atts['text']=='no-text')返回;
$output=
'
“.$atts['img']”。
“.$atts['user']” “.$atts['text']” '; 返回$output; } 添加_快捷码('hire_equipment'、'hire_equipment_func')
[hire\u equipment img=”“user=“Test Float”text=“可在任何地方使用”]
如果未设置其中一个属性,则将不返回任何内容,否则将在
div中返回每个属性

   function hire_equipment_func($atts) {
        $atts = array_change_key_case((array)$atts, CASE_LOWER);
        if (!isset($atts['img']) || !isset($atts['user']) || !isset($atts['text'])) return; 

        $output =
            '<div>        
                <div>' . $atts['img'] . '</div><br />
                <div>' . $atts['user'] . '</div>
                <div>' . $atts['text'] . '</div>
            </div>';

        return $output;
    }
    add_shortcode( 'hire_equipment', 'hire_equipment_func' );
功能租用设备功能($atts){
$atts=数组\更改\键\大小写((数组)$atts,大小写\降低);
如果(!isset($atts['img'])| |!isset($atts['user'])| |!isset($atts['text']))返回;
$output=
'        
“.$atts['img']”。
“.$atts['user']” “.$atts['text']” '; 返回$output; } 添加_快捷码('hire_equipment'、'hire_equipment_func');
这将删除未指定的部分,因此如果您将此PHP片段放入functions.PHP文件(最好是在您的子主题中):

您将获得:

  
Test Float
Can be used anywhere
例2:

[hire_equipment img="/file/path/image.svg" user="Test Float" text="Can be used anywhere"]
屈服

/file/path/image.svg

Test Float
Can be used anywhere
例3:

[hire_equipment img="/file/path/image.svg" text="Can be used anywhere"]
输出

/file/path/image.svg

Can be used anywhere
例4:

[hire_equipment img="" user="" text=""]
显示

由标记中的换行符引起


如果您想知道,PHP_EOL是一个PHP常量,表示您的平台的正确“行尾”符号。

不过,我还有一个问题。如果我没有在其中一个属性中添加任何内容,它仍然会显示在我的网页上。如何删除没有属性数据的部分??对每个部分使用
isset
。您能给我举个例子吗?还在学习Wordpress哈哈:)谢谢!我的答案中提供了一个使用isset()的示例。我的代码已更改以适应更多信息。下面是新代码。我希望它能像“如果”中那样工作,某些属性已满,则显示$output1。“如果”包含其他特定属性,则显示$output2。检查一下,看看你是否知道出了什么问题:
/file/path/image.svg

Can be used anywhere
[hire_equipment img="" user="" text=""]