WordPress简码不显示结果



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

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

1: 脚本

2:网页

function hire_equipment_func($atts) {
extract( shortcode_atts( array(
'img' => 'no-img',
'user' => 'no-user',
		'text' => 'no-text',
), $atts ) );
if ( $atts['img'] == '' || $atts['user'] == '' || $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' );
[hire_equipment img="" user="Test Float" text="Can be used anywhere"]

对此问题的任何帮助将不胜感激!!

亲切问候 耶西

如果未设置其中一个属性,这将不返回任何内容,否则将返回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' );

function hire_equipment_func($atts) {
extract( shortcode_atts( array(
'img' => 'no-img',
'user' => 'no-user',
		'text' => 'no-text',
), $atts ) );
if ( $atts['img'] == 'no-img' || $atts['user'] == 'no-user' || $atts['text'] == 'no-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' );
[hire_equipment img="" user="Test Float" text="Can be used anywhere"]

好的,出于某种原因,我将每个属性中的相同文本(例如属性"img"中的"no-img"(放入 IF 语句的每个文本区域中,并且它起作用了!

所以我回答了我自己的问题!但是,我仍然有兴趣想知道为什么它会以这种方式工作。一旦有人帮助我理解其背后的概念原因,我愿意将问题标记为已回答。

谢谢! 耶西

这将删除未指定的部分,因此,如果您将 PHP 片段扔到函数.php文件中(最好在您的子主题中(:

function hire_equipment_func( $atts ) {
extract(
shortcode_atts( 
array(
'img'    => 'no-img'
, 'user' => 'no-user'
, 'text' => 'no-text'
)
, $atts 
) 
);
$output = '<div>' . PHP_EOL;
if( isset( $atts[ 'img' ]  ) ) { $output .= '    <div>' . $atts[ 'img' ]  . '</div><br>' . PHP_EOL; }
if( isset( $atts[ 'user' ] ) ) { $output .= '    <div>' . $atts[ 'user' ] . '</div>'     . PHP_EOL; }
if( isset( $atts[ 'text' ] ) ) { $output .= '    <div>' . $atts[ 'text' ] . '</div>'     . PHP_EOL; }
$output .= '</div>';
return $output;
}
add_shortcode( 'hire_equipment', 'hire_equipment_func' );

并将此WordPress短代码放在您的页面上:

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

您将获得:

  
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常量,表示您的平台的正确"行尾"符号。

最新更新