我一直在寻找解决方案,但找不到。
我创建了一个显示社交链接的简码,具体取决于以下类型的代码:
function ns_social_buttons_dynamic($atts,$content,$tag) {
$facebookUrl = get_option('ns-company-facebook');
$messengerUrl = get_option('ns-company-messenger');
if (!empty($facebookUrl)) {
$fb = '<a class="btn-ns btn-ns-second m-1 bg-facebook" href="https://facebook.com/'.$facebookUrl.'" role="button" target="blank"><i class="fab fa-facebook-f"></i></a>';
};
if (!empty($messengerUrl)) {
$msngr = '<a class="btn-ns btn-ns-second m-1 bg-messenger" href="https://m.me/'.$messengerUrl.'" role="button" target="blank"><i class="fab fa-facebook-messenger"></i></a>';
}
$nsSocial = $fb . $msngr;
//get admin url
$addLink = get_site_url( $blog_id, 'wp-admin/', $scheme );
//default value of shortcodes shows all social buttons
$values = shortcode_atts(array(
'type' => 'all',
'output' => 'raw',
),$atts);
//social button based on type
$output = '';
if($values['type'] == 'all' and !empty($nsSocial)){
$output = $nsSocial;
}
else if($values['type'] == 'facebook' and !empty($facebookUrl)){
$output = $fb;
}
else if($values['type'] == 'messenger' and !empty($messengerUrl)){
$output = $msngr;
}
else {
$output = 'field is empty <a href=' . $addLink . 'admin.php?page=ns-settings' .' >add username</a>';
}
return $output;
}
add_shortcode( 'ns-social', 'ns_social_buttons_dynamic' );
此代码可以正常工作,例如[ns-socia type='facebook']
将输出右键。
现在我需要的是是否像[ns-socia type='facebook' output='raw']
之后的短代码仅显示url而不显示按钮。
我尝试了以下操作:
//social button based on type
$output = '';
if($values['type'] == 'all' and !empty($nsSocial)){
$output = $nsSocial;
}
else if($values['type'] == 'facebook' and $values['output'] == 'raw' and !empty($facebookUrl)){
$output = $facebookUrl;
}
else if($values['type'] == 'messenger' and $values['output'] == 'raw' and !empty($messengerUrl)){
$output = $messengerUrl;
}
else {
$output = 'field is empty <a href=' . $addLink . 'admin.php?page=ns-settings' .' >add username</a>';
}
return $output;
我不确定我是否使用条件php条件语句来正确执行操作...