dongyao1895 2012-06-23 10:36
浏览 102
已采纳

如果显示自定义字段值,则显示另一个自定义字段值

I have a Wordpress managed web site at http://www.urbanvision.org.uk/ I have built it and everything works like I want it to and I'm happy with the outcome as it is my first fully built Wordpress web site.

I've become stuck on a request I had at the start of the week.

We have a Properties for Sale page (http://www.urbanvision.org.uk/services/property-services/properties-for-sale/) the items on this page link to PDF downloads of plans and property details. However we now have a number of properties that need to be added that link not the PDF but to an external link.

The problem I have is the page template relies on custom fields managed by the plugin Advanced Custom Fields, the field to upload the PDF is a file upload field which will take a PDF but not an URL to another page or site.

I have tried to switch the custom field to an URL rather than an upload screen but not keen on this for 2 reasons, 1) i'd have go back through the other properties and copy the url into the changed field and 2) it becomes a little more difficult for colleagues to update.

I have also tried introducing a separate a field and working out which custom field should be pulled in:

If PDF file exists in property_details pull in the URL to PDF. If URL exists in property_details_url pull in URL entered.

There are two parts of each post that need to link to further details (PDF or external URL). They are the thumbnail image and the view details link.

The code I had before (just linking to the 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 />

The Code I have changed it to (still can't get it to work):

    <?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 } ?>

I also had a look at pulling directly from the MySQL database that Wordpress is using but really struggled to get the page to display without error.

As always, any help would be greatly appreciated.

  • 写回答

1条回答 默认 最新

  • dpsfay2510 2012-06-23 13:24
    关注

    your on the right track, but what to do is assign your customfield to a variable.

    ie:

    <?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 } ?>
    

    hopefully the check should fine that the property_details_url has somthing other than nothing, it will show the link, if not it will show the other piece of code?

    Marty

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器