doucheng3811 2014-10-21 06:50
浏览 44
已采纳

删除美元符号和自定义字段“价格”之间的空白

I am using custom fields to display prices (except for the dollar sign). My goal is to code the dollar sign within the theme, but I encountered an issue.

The reason I am using this approach is because if I enter a dollar sign in the custom field I will not be able to use 'orderby' => 'meta_value_num'
And I need to use a conditional statement to display the dollar sign because not all posts will have prices.

The code below results in

$ 9.75

Notice the white space between the dollar sign and the number "9". How can I remove the white space? Or is there an alternative method to code a dollar sign using a conditional statement ?

<?php if( get_post_meta($post->ID, 'price', true) ) { ?>
$
<?php } ?>

<?php echo get_post_meta( get_the_ID(), 'price', true); ?>
  • 写回答

1条回答 默认 最新

  • douzhi9635 2014-10-21 06:55
    关注

    Removed the whitespaces in the code itself and optimized it a bit (didn't understand why would you have a condition for the dollar sign, but not for the price itself), this should work:

    <?php if( get_post_meta($post->ID, 'price', true) ) {
       echo '$', get_post_meta( get_the_ID(), 'price', true);
    } ?>
    

    Where was space coming from in the original code:

    <?php if( get_post_meta($post->ID, 'price', true) ) { ?>
    $<!- HERE ->
    <?php } ?>   <!- HERE ->
    <!- AND HERE ->
    <?php echo get_post_meta( get_the_ID(), 'price', true); ?>
    

    See the idea here: http://jsfiddle.net/93mcu7sj/

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

报告相同问题?

悬赏问题

  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分