1

現在、結果を取得している php の次の 2 つのブロックがあります。

<?php
    $webtech = article_custom_field('web-tech');
    if ( !empty($webtech) ) :
?>
    <div class="tech-list">
        <div class="tech-title">Technologies:</div>
        <ul class="tech-ul"><?php echo $webtech; ?></ul>
    </div>
<?php
    endif;
?>

<?php
    $url = article_custom_field('site-url');
    elseif ( !empty($url) ) :
?>
    <div class="site-url"><a href="<?php echo $url; ?>" target="_blank">Visit</a></div>
<?php
    endif;
?>

次のように、それらを組み合わせて 1 つのブロックを出力したいと考えています。

<div class="tech-list">
    <div class="tech-title">Technologies:</div>
    <ul class="tech-ul"><?php echo $webtech; ?></ul>
    <div class="site-url"><a href="<?php echo $url; ?>" target="_blank">Visit</a></div>
</div>

以下を満たす必要があります。

web-techがあれば出力する。存在しない場合、サイト URL を出力しません。

web-techがあれば出力する。site-url が存在する場合は、それを出力します。

site-url が存在する場合は、それを出力します。存在しない場合は web-tech を出力しないでください。

どちらの変数も存在しない場合、含まれている div はまったく出力されません。

それを行うための明白な方法がありませんか?些細なことのように思えますが、if/else/elseif ステートメントを並べることができません。

4

3 に答える 3

2

それらが存在するコンテナを出力する前に、両方の変数をチェックする必要があるようです。

<?php
    $webtech = article_custom_field('web-tech');
    $url = article_custom_field('site-url');

    if ( !empty($webtech) || !empty($url))
    {
?>
    <div class="tech-list">
<?php
       if ( !empty($webtech) )
       {
?>
        <div class="tech-title">Technologies:</div>
        <ul class="tech-ul"><?php echo $webtech; ?></ul>
<?php
       }

       if ( !empty($url) )
       {
?>
        <div class="site-url"><a href="<?php echo $url; ?>" target="_blank">Visit</a></div>
<?php

       }
?>
    </div>
<?php
    }
?>
于 2013-10-18T15:41:45.960 に答える
2

次のように、出力を変数に格納できます。

<?php
    $output = '';
    $webtech = article_custom_field('web-tech');
    if ( !empty($webtech) ) :
        $output .= '<div class="tech-title">Technologies:</div>'
            . '<ul class="tech-ul">' . $webtech . '</ul>';
    endif;

    $url = article_custom_field('site-url');
    if(!empty($url)) :
        $output .= '<div class="site-url"><a href="' . $url . '" target="_blank">Visit</a></div>';
    endif;

    if($output != ''):
        echo '<div class="tech-list">';
        echo $output;
        echo '</div>';
    endif;
?>

このようにして、出力変数に何かが設定されている場合にのみ、何かが表示されます。

これで問題は解決しますか?

于 2013-10-18T15:42:22.927 に答える
0
if (a or b) then {
  OpenTechTitle;
  if (a) SetAtext;
  if (b) SetBtext;
  CloseTechTitle;
}
于 2013-10-18T15:20:08.507 に答える