如何在此代码中格式化'else'?

问题描述 投票:-1回答:1

我正在学习编码,我知道这可能是一个非常容易回答的问题,但是当这里没有数据时,我只是想不出如何回声。

我知道这与使用'else'有关,但是我应该放在哪里?这段代码基本上可以召唤社交媒体链接,我希望两者都取决于没有数据显示。我认为在第一段中必须有else代码应该去的某个地方,但我真的不知道在哪里。

<?php if($instance['display_desc']) : ?>
<?php $description = get_the_author_meta( 'description', $user_id ); ?>
<?php echo wpautop( $this->trim_chars( $description, $instance['limit_chars'] ) ); ?>

<?php $instagram = get_the_author_meta('instagram'); ?>
<?php $twitter = get_the_author_meta('twitter'); ?>

<?php echo
$authsocial .= "<a class=\"author-ig\" href=\"https://instagram.com/" . $instagram . "\" target=\"_blank\" rel=\"nofollow\">@" . get_the_author_meta('instagram') . " </a>"
?>

<br />
<?php echo
"<a class=\"author-tw\" href=\"https://twitter.com/" . $twitter . "\" target=\"_blank\" rel=\"nofollow\">@" . get_the_author_meta('twitter') . " </a>";
?>

<?php endif; ?>

php wordpress if-statement
1个回答
0
投票

if(...) {...} else {...}

if(...): else: ... endif;

而且我不会在每行标记上乱扔很多东西。可读性下降。

您的代码重塑形式:

<?php if ($instance['display_desc']): $description = get_the_author_meta( 'description', $user_id ); echo wpautop( $this->trim_chars( $description, $instance['limit_chars'] ) ); $instagram = get_the_author_meta('instagram'); $twitter = get_the_author_meta('twitter'); echo $authsocial .= "<a class=\"author-ig\" href=\"https://instagram.com/" . $instagram . "\" target=\"_blank\" rel=\"nofollow\">@" . get_the_author_meta('instagram') . " </a>"; ?><br /> <a class="author-tw" href="https://twitter.com/<?php echo $twitter ?>" target="_blank" rel="nofollow">@<?php echo get_the_author_meta('twitter'); ?></a> <?php else: ?> ELSE_HTML_GOES_HERE <?php endif; ?>

© www.soinside.com 2019 - 2024. All rights reserved.