Как мне отформатировать «еще» в этот код? - PullRequest
0 голосов
/ 19 марта 2020

Я учусь кодировать, и я знаю, что это, вероятно, очень простой вопрос, но я просто не могу понять, как выводить эхо, когда здесь нет данных.

Я знаю это связано с использованием 'else', но куда мне его поместить? Этот код в основном вызывает ссылки на социальные сети, и я хочу, чтобы оба зависели от того, не показывать ли данные. Я думаю, что в первом абзаце должно быть где-то, что код else должен go, но я действительно не знаю, где.

<?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; ?>

--

Igor helped me loads, but I wonder why this isn't working:

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

if (!empty($twitter)): ?><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 endif; ?>

<?php else: ?>
ELSE_HTML_GOES_HERE
<?php endif; ?>

1 Ответ

1 голос
/ 19 марта 2020

Это просто! Вы можете использовать

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>";

  if (!empty($twitter)): ?><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 endif; ?>
<?php else: ?>
ELSE_HTML_GOES_HERE
<?php endif; ?>
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...