属性数组中有两个值的短代码问题
Problem with short-code with two value from the attributes array
我在 wordpress 环境中为 运行 创建的简码有问题。
我不是简码专家,就是这样:
add_shortcode('key_value' , 'key_value' );
function key_value( $atts, $content = null ) {
$atts = shortcode_atts( array(
'title' => '',
'url' => '',
), $atts );
if ($atts['title']) {
$html = '<li><strong>'.$atts['title'].': </strong>'.$content.'</li>';
} elseif ($atts['url'] && $atts['title']) {
$html = '<li><a href="'.$atts['url'].'" target="_blank">'.$atts['title'].'</a></li>';
}
return $html;
}
我想 return 第二个 if 语句,因为我有一堆这样的简码:
[key_value title="Delivery"]Fast[/key_value]
[key_value title="Duration"]2 weeks[/key_value]
它们工作正常,但最后一个:
[key_value title="Info Session" url="/info/"][/key_value]
不起作用,我不明白为什么,因为如果我 var_dump 我的数组 $atts,我就能看到两个值。
有什么建议吗?
您的代码正在进入第一个条件。更改逻辑以首先执行 else
条件,或者换句话说,如果两个属性都使用它,则仅检查标题:
if ($atts['url'] && $atts['title']) {
$html = '<li><a href="'.$atts['url'].'" target="_blank">'.$atts['title'].'</a></li>';
} elseif ($atts['title']) {
$html = '<li><strong>'.$atts['title'].': </strong>'.$content.'</li>';
}
我在 wordpress 环境中为 运行 创建的简码有问题。
我不是简码专家,就是这样:
add_shortcode('key_value' , 'key_value' );
function key_value( $atts, $content = null ) {
$atts = shortcode_atts( array(
'title' => '',
'url' => '',
), $atts );
if ($atts['title']) {
$html = '<li><strong>'.$atts['title'].': </strong>'.$content.'</li>';
} elseif ($atts['url'] && $atts['title']) {
$html = '<li><a href="'.$atts['url'].'" target="_blank">'.$atts['title'].'</a></li>';
}
return $html;
}
我想 return 第二个 if 语句,因为我有一堆这样的简码:
[key_value title="Delivery"]Fast[/key_value]
[key_value title="Duration"]2 weeks[/key_value]
它们工作正常,但最后一个:
[key_value title="Info Session" url="/info/"][/key_value]
不起作用,我不明白为什么,因为如果我 var_dump 我的数组 $atts,我就能看到两个值。
有什么建议吗?
您的代码正在进入第一个条件。更改逻辑以首先执行 else
条件,或者换句话说,如果两个属性都使用它,则仅检查标题:
if ($atts['url'] && $atts['title']) {
$html = '<li><a href="'.$atts['url'].'" target="_blank">'.$atts['title'].'</a></li>';
} elseif ($atts['title']) {
$html = '<li><strong>'.$atts['title'].': </strong>'.$content.'</li>';
}