dongqiao8421
2017-04-10 19:06 阅读 95
已采纳

在shortcode中插入变量值

I want to use the shortcode below to add a review box to posts in my wordpress template:

<?php echo do_shortcode('[rwp-review id="X"]'); ?>

Where X is the Review ID of the review box for each post.

To get the Review ID, I have the code below but it is not working. $postid gets the current post ID. $box result returns array values for the review box one of which is the Review ID. If I echo $reviewid, I get the Review ID which can be 0, 1, 2, 3 and upwards. I then tried to use $reviewid in the final shortcode but It is not working. I have very little PHP knowledge so I think I inserted the code the wrong way.

<?php
        $postid = get_the_ID();
        $box = RWP_API::get_post_reviews_boxes( $postid, false );
        $reviewid = $box[0]['review_id'];

    ?>

    <?php echo do_shortcode('[rwp-review id=". $reviewid . "]'); ?>

Can anyone suggest the best approach to this?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

1条回答 默认 最新

  • 已采纳
    douluo3256 douluo3256 2017-04-10 19:27

    Try:

    <?php echo do_shortcode('[rwp-review id="'. $reviewid . '"]'); ?>

    or

    <?php echo do_shortcode("[rwp-review id='{$reviewid}']"); ?>

    点赞 评论 复制链接分享

相关推荐