0

私は次のコードを持っています:

<?php $buycheck = get_post_meta($post->ID, 'buy-link', true); ?>

            <?php if ( $buycheck ) : ?>
                <div class="section-title sidebar span5">
                    <h5>Get This Release</h5>
                </div>

            <?php else : ?>

                <div class="section-title sidebar span5">
                    <h5>More Releases</h5>
                </div>

            <?php endif; ?>

私のコードの後半で、購入リンクが存在しない場合、つまりそのフィールドにデータがない場合は、何かを実行し、そうでない場合は別のことを実行すると言いたいです。

これを行う方法がわからない!感謝します!

(ちなみに、私は最初にこの質問をWordpress Stack Exchangeに投稿しました。WordpressよりもPHPブール論理に関係していると思われるため、そこでクローズに投票されました-https ://wordpress.stackexchange.com/questions/60387/how-do-i -do-if-post-meta-does-not-exist#comment78412_60387

4

2 に答える 2

1

購入リンクが存在するかどうかを後で確認できるグローバル変数を設定できます。

<?php

$buycheck = get_post_meta($post->ID, 'buy-link', true);
$GLOBALS['buy_link_exists'] = !empty($buycheck);

?>

<?php if ( $buycheck ) : ?>
<div class="section-title sidebar span5">
    <h5>Get This Release</h5>
</div>

<?php else : ?>

<div class="section-title sidebar span5">
    <h5>More Releases</h5>
</div>

<?php endif; ?>

その後、コードで次のようにします。

<?php if ($GLOBALS['buy_link_exists'])): ?>
    it exists, do one thing
<?php else: ?>
    it does not exist, do something else
<?php endif; ?>

実際の値が必要な場合は、戻り値を含むグローバルを設定してget_post_meta、実際の値を使用できるようにすることができます。

于 2012-07-31T22:25:13.803 に答える
1
<?php if($buycheck ==''){ /*stuff*/ } ?>

これは $buycheck をレンダリングし、空の場合==''何もありません。

于 2012-07-31T22:28:09.420 に答える