dongshou1856 2015-07-28 23:28
浏览 20
已采纳

自定义Wordpress字段仅在完成时显示

I created a custom-post-type using the Pods plugin. One of the fields is a "website" field. In single-portfolio.php, I have the following code in order to display the website field info.

<p>
<?php $website = get_post_meta($post->ID, 'website', true);?> Website: <a target="_blank" href="<?php echo $website; ?>"><?php echo preg_replace('|https?://|', '', $website ); ?></a><?php $site = preg_replace('{/$}', '', $website); ?>
</p>

2 Questions:

  1. This creates a link url, which when clicked on, the url is preceded by MY site url, and obviously links to a 404 page. What can I change to get ONLY the field's info url?

  2. How can I get the word "Website:" (which precedes the actual field info) to appear in the site ONLY if the field was completed in the backend. Meaning, if this particular project does not have any website info associated with it, the text "Website:" should not appear at all on the site page?

  • 写回答

1条回答 默认 最新

  • doujupa7567 2015-07-29 06:02
    关注

    To conditionally display the link (issue 2) check if the $website variable has a value:

    <?php $website=get_post_meta($post->ID,'website',true);
    if($website):?>
        Website: <a target="_blank" href="<?php echo $website; ?>"><?php echo preg_replace('|https?://|', '', $website ); ?></a>
    <?php $site = preg_replace('{/$}', '', $website);
    endif;?>
    

    Issue 1 sounds like your URL doesn't start with http://. If it doesn't the browser will assume it's a relative link. If you can't know in advance whether the $website string will start with http://, implement a function like the one provided in this answer: https://stackoverflow.com/a/2762083/4772280.

    Once you've added that function to your functions.php file add this line to your code, after you assign a value to $website:

    $website=addhttp($website);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?