So i have a shortcode called [ kerrigan
] and I want to have 3 atts where when used each one will display different content for the shortcode so for example [ kerrigan link="true"
] it shows a certain return, when I use [ kerrigan icon="true"
] it shows a different return same for [ kerrigan image="true"
] than I would like to have just [ kerrigan
] have it's own content to return also. So pretty much have it output something different depending on what I put in there as the atts
add_shortcode('kerrigan', 'kerring');
function kerrigan( $atts, $content = null )
{
array(
'link' => 'true',
'icon' => 'true',
'image' => 'true',
);
if($link == 'true'){
return 'display content 1';
}
if($icon == 'true'){
return 'display content 2';
}
if($image == 'true'){
return 'display content 3';
}
}
I'm still learning PHP so my if statment syntax I"m sure are a bit off.
if($link = 'true'){
}
should be
if($link == 'true'){
}
And so on for others !!
= is for assignment
== is for comarison.
function kerring($atts)
{
if(is_array($atts)){
foreach($atts as $key=>$value){
switch ($key){
case 'link': if($value == 'true') return 'content for link';
case 'icon': if($value == 'true') return 'content for icon';
case 'image': if($value == 'true') return 'content for image';
default: return 'some default content for other atts';
}
}
}
return 'content without atts';
}
add_shortcode('kerrigan', 'kerring');
added array check, it works, but i think its a misuse of foreach
statement, another aproach would be extracting $atts
as variables
function kerring($atts)
{
extract($atts);
if(isset($link)){
if($link=='true'){
return 'content for link';
}
}
if(isset($icon)){
if($icon=='true'){
return 'content for icon';
}
}
if(isset($image)){
if($image=='true'){
return 'content for image';
}
}
return 'content without atts';
}
add_shortcode('kerrigan', 'kerring');
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.