[英]How I can use php if statment in wordpress shortcode?
I will try to make a WordPress plugin. 我将尝试制作一个WordPress插件。 My all code is ok but I can't understand how I use if else statement in custom post shortcode.
我的所有代码都没问题,但我无法理解我如何在自定义帖子短信中使用if else语句。
global $post;
$accordion_icon = get_post_meta($idd, 'accordion_icon', true);
$list .= '
<div class="collapse-card">
<div class="title">
<i class="fa '.$accordion_icon.' fa-2x fa-fw"></i>
<strong>'.get_the_title().'</strong>
</div>
<div class="body">
<p>'.get_the_content().'</p>
</div>
</div>
';
It's my present code. 这是我现在的代码。 Hear have a icon option in title div .I will want to when I put any custom icon code then show the custom icon and when I don't put any custom icon code then show a common icon.
听到标题div中有一个图标选项。我想在我放置任何自定义图标代码然后显示自定义图标时,当我没有放任何自定义图标代码时,我会想要显示一个共同的图标。 I can understand here need to use if else statement but I don't understand how I use if else statement in my code.
我可以理解这里需要使用if else语句,但我不明白我如何在我的代码中使用if else语句。
$list .= '
<div class="collapse-card">
<div class="title">
'<?php if($accordion_icon) : ?>'
<i class="fa '.$accordion_icon.' fa-2x fa-fw"></i>
'<?php else : ?>'
<i class="fa fa-question-circle fa-2x fa-fw"></i>
'<?php endif; ?>'
<strong>'.get_the_title().'</strong>
</div>
<div class="body">
<p>'.get_the_content().'</p>
</div>
</div>
';
I used like that but it's not working.Show error . 我曾经这样使用但它不起作用。显示错误。
So now how I can use if else statement ? 那么现在我如何使用if else语句?
You can do it either like this (short way): 你可以这样做(简短的方法):
$list .= '<div class="collapse-card">
<div class="title">';
if($accordion_icon)
$list .= '<i class="fa '.$accordion_icon.' fa-2x fa-fw"></i>">';
else
$list .= '<i class="fa fa-question-circle fa-2x fa-fw"></i>';
Or like this (long way): 或者像这样(很长的路):
$list .= '<div class="collapse-card">
<div class="title">';
if($accordion_icon){
$list .= '<i class="fa '.$accordion_icon.' fa-2x fa-fw"></i>">';
}else{
$list .= '<i class="fa fa-question-circle fa-2x fa-fw"></i>';
}
Note: You can only use the short way for 1 line. 注意:您只能使用1行短路。 If your body statement exceeds 1 line you require the curly braces.
如果你的身体陈述超过1行你需要花括号。
You can also used something called the coditional operator , but I don't think that's what you're looking for, for now. 您也可以使用称为编码运算符的东西,但我认为这不是您现在正在寻找的东西。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.