drake900918 2015-05-21 15:43
浏览 53
已采纳

如果Field为空,则显示某些div

I have the following code but it is not working.

Basically I want it so if the field movie_title_2 is empty I want it to add the , but if it is not empty to show just . I am using wordpress and Advanced Custom Fields plugin. Thanks!

<div class="homepage-opacity">
    <div class="movie-container">
    <?php if (get_field('movie_title_2') && get_field('movie_title_2') != "") { ?>

        <div class="movie-area alone">
            <div class="movie-poster">          
            <?php  if ( get_field('movie_image_poster') ) : ?>
                <img src="<?php the_field('movie_image_poster'); ?>" alt="<?php the_field('movie_image_poster'); ?>}">
            <?php endif; ?>
            </div>
            <div class="movie-info">
            <?php
            if(get_field('movie_title'))
            {
                echo '<h1 class="movie-title">' . get_field('movie_title') . '</h1>';
            }
            ?>  
            <?php
            if(get_field('movie_dates_&_times'))
            {
                echo '<p class="movie-times">' . get_field('movie_dates_&_times') . '</p>';
            }
            ?>  
            <?php  if ( get_field('trailer_link') ) : ?>
                <a class="movie-trailer" href="<?php the_field('trailer_link'); ?>" target="_blank">View the Trailer</a>
            <?php endif; ?>

            </div>
        </div>

    <?php } ?>
    <?php if (get_field('movie_title_2') && get_field('movie_title_2') == "") { ?>

        <div class="movie-area">
            <div class="movie-poster">          
            <?php  if ( get_field('movie_image_poster') ) : ?>
                <img src="<?php the_field('movie_image_poster'); ?>" alt="<?php the_field('movie_image_poster'); ?>}">
            <?php endif; ?>
            </div>
            <div class="movie-info">
            <?php
            if(get_field('movie_title'))
            {
                echo '<h1 class="movie-title">' . get_field('movie_title') . '</h1>';
            }
            ?>  
            <?php
            if(get_field('movie_dates_&_times'))
            {
                echo '<p class="movie-times">' . get_field('movie_dates_&_times') . '</p>';
            }
            ?>  
            <?php  if ( get_field('trailer_link') ) : ?>
                <a class="movie-trailer" href="<?php the_field('trailer_link'); ?>" target="_blank">View the Trailer</a>
            <?php endif; ?>

            </div>
        </div>
        <div class="movie-area">
            <div class="movie-poster">          
            <?php  if ( get_field('movie_image_poster_2') ) : ?>
                <img src="<?php the_field('movie_image_poster_2'); ?>" alt="<?php the_field('movie_image_poster_2'); ?>}">
            <?php endif; ?>
            </div>
            <div class="movie-info">
            <?php
            if(get_field('movie_title_2'))
            {
                echo '<h1 class="movie-title">' . get_field('movie_title_2') . '</h1>';
            }
            ?>  
            <?php
            if(get_field('movie_dates_&_times_2'))
            {
                echo '<p class="movie-times">' . get_field('movie_dates_&_times_2') . '</p>';
            }
            ?>  
            <?php  if ( get_field('trailer_link_2') ) : ?>
                <a class="movie-trailer" href="<?php the_field('trailer_link_2'); ?>" target="_blank">View the Trailer</a>
            <?php endif; ?>

            </div>
        </div>
        <?php } ?>
    </div>
</div>
  • 写回答

1条回答 默认 最新

  • donglun1020 2015-05-21 15:49
    关注

    try:

      <?php 
        $variable = get_field('movie_title_2');
    
        if ($variable) { ?>
    
    <h1>Add html just like you would normally do</h1>
    
    <?php } else { ?>  
    
    <h1>Add different html here when the variable is empty</h1>
    
    
        <?php } ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)
  • ¥50 mac mini外接显示器 画质字体模糊
  • ¥15 TLS1.2协议通信解密
  • ¥40 图书信息管理系统程序编写
  • ¥20 Qcustomplot缩小曲线形状问题
  • ¥15 企业资源规划ERP沙盘模拟
  • ¥15 树莓派控制机械臂传输命令报错,显示摄像头不存在
  • ¥15 前端echarts坐标轴问题
  • ¥15 ad5933的I2C