【问题标题】:Hide php link if the field is empty?如果字段为空,隐藏 php 链接?
【发布时间】:2014-09-05 11:46:36
【问题描述】:

我试图仅在链接具有值时才显示它。

如果the_field imdb不为空,如何获取图片?

<a href="<?php the_field('imdb'); ?>" >
  <img style="width:60px;"src="/img/link.png" /></a>

【问题讨论】:

    标签: php wordpress plugins


    【解决方案1】:

    使用get_field(); 而不是the_filed();

    if(!empty(get_field("fildname"))){
    #your code hear
    }
    

    【讨论】:

      【解决方案2】:

      试试:-

      if(!empty(the_field('imdb'))){
          <a href="<?php the_field('imdb'); ?>" ><img style="width:60px;"src="/img/link.png" /></a>
      }
      

      【讨论】:

        【解决方案3】:

        使用isset

        if(isset(the_field('imdb'))&&!empty(the_field('imdb'))){
        <img src=""/>
        }
        

        【讨论】:

        • 不需要isset() 来自empty() 文档empty is essentially the concise equivalent to !isset($var) || $var == false
        【解决方案4】:

        由于我不想在旧帖子上显示 img 或链接,我通过以下方式解决了它:

        <?php 
        
        // assign image
        $imdbimg = "<img src='http://mydomain.com/IMDb.png' class='imdb' />";
        
        // imdb link from custom field
        $imdblink = get_field('imdb');
        
        ?>
        
        // display img and link on post newer then id 16
        <?php global $post;
        if ( $post->ID >= 16 ){  
        echo '<a href="' . $imdblink . '" target="_blank"> ' . $imdbimg . '</a>';
        }    
        ?>
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2013-10-02
          • 2019-10-20
          • 2021-07-10
          • 2013-06-26
          • 2013-09-03
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多