【问题标题】:If Custom Field Value exists display, if not display another Custom field value如果存在自定义字段值则显示,如果不存在则显示另一个自定义字段值
【发布时间】:2014-03-30 09:12:24
【问题描述】:

我在http://www.urbanvision.org.uk/ 有一个由 Wordpress 管理的网站,我已经建立了它,一切都按照我的意愿运行,我对结果很满意,因为它是我第一个完全建立的 Wordpress 网站。

我在本周初的一个请求上卡住了。

我们有一个待售物业页面 (http://www.urbanvision.org.uk/services/property-services/properties-for-sale/) 此页面上的项目链接到 PDF 下载计划和物业详细信息.但是,我们现在需要添加许多属性,这些属性不是链接 PDF,而是链接到外部链接。

我遇到的问题是页面模板依赖于由插件高级自定义字段管理的自定义字段,上传 PDF 的字段是一个文件上传字段,它将采用 PDF 但不是另一个页面或站点的 URL。

我尝试将自定义字段切换到 URL 而不是上传屏幕,但出于 2 个原因不热衷于此,1)我会返回其他属性并将 url 复制到更改的字段中,2 ) 同事更新变得有点困难。

我也尝试过引入一个单独的 a 字段并确定应该拉入哪个自定义字段:

如果 PDF 文件存在于 property_details 中,则拉入 PDF 的 URL。 如果 URL 存在于 property_details_url 中,则拉入输入的 URL。

每个帖子的两个部分需要链接到更多详细信息(PDF 或外部 URL)。它们是缩略图和查看详细信息链接。

我之前的代码(只是链接到 PDF):

<?php
$featuredPosts = new WP_Query();
$featuredPosts->query('showposts=20&cat=13');
while ($featuredPosts->have_posts()) : $featuredPosts->the_post(); ?>

<div class="literaturedescription">
<a href="<?php the_field('property_details'); ?>" title="<?php the_field('property_title'); ?>">
<img src="<?php the_field('property_thumbnail'); ?>" width="220px" height="150px" alt="<?php the_field('property_title'); ?>" /></a>
<p><strong><?php the_field('property_title'); ?></strong><br /><?php the_field('property_excerpt'); ?> <span style="color:red;font-weight:bold;"><?php the_field('property_status'); ?></span>

<br /><a href="<?php the_field('property_details'); ?>" target="_blank" title="<?php the_field('property_title'); ?>">&gt; &gt; View Details</a></p><br />

我已将其更改为的代码(仍然无法正常工作):

    <?php $featuredPosts = new WP_Query(); 
$featuredPosts->query('showposts=20&cat=12');
while ($featuredPosts->have_posts()) : $featuredPosts->the_post(); ?>

<div class="literaturedescription">
<a href="<?php the_field('property_details'); ?>" title="<?php the_field('property_title'); ?>">
<img src="<?php the_field('property_thumbnail'); ?>" width="220px" height="150px" alt="<?php the_field('property_title'); ?>" /></a>
<p><strong><?php the_field('property_title'); ?></strong><br /><?php the_field('property_excerpt'); ?> <span style="color:red;font-weight:bold;"><?php the_field('property_status'); ?></span>


<?php if(get_field('property_details_url')){ ?>

<br /><a href="<?php the_field('property_details_url'); ?>" target="_blank" title="<?php the_field('property_title'); ?>">&gt; &gt; View Details</a></p><br />

<?php } else { ?>

<br /><a href="<?php the_field('property_details'); ?>" target="_blank" title="<?php the_field('property_title'); ?>">&gt; &gt; View Details</a></p><br />

<?php } ?>

我还查看了直接从 Wordpress 正在使用的 MySQL 数据库中提取数据,但确实很难让页面正确显示。

一如既往,我们将不胜感激。

【问题讨论】:

    标签: php wordpress if-statement custom-fields


    【解决方案1】:

    您在正确的轨道上,但要做的是将您的自定义字段分配给一个变量。

    即:

    <?php
    $prop_det_url = get_field('property_details_url');
    if($prop_det_url!=''){ ?>
    
    <br /><a href="<?php echo $prop_det_url; ?>" target="_blank" title="<?php the_field('property_title'); ?>">&gt; &gt; View Details</a></p><br />
    
    <?php } else { ?>
    
    <br /><a href="<?php the_field('property_details'); ?>" target="_blank" title="<?php the_field('property_title'); ?>">&gt; &gt; View Details</a></p><br />
    
    <?php } ?>
    

    希望检查应该没问题,property_details_url 有其他东西,它会显示链接,如果没有,它会显示另一段代码?

    马蒂

    【讨论】:

      猜你喜欢
      • 2017-05-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-17
      相关资源
      最近更新 更多