dongtan7201 2017-09-13 19:32
浏览 335
已采纳

在wordpress页面中嵌入Tradingview小部件

I am an extremely beginner of Wordpress. I need to add a tradingview widget on my Wordpress page. Code as below.

<!-- TradingView Widget BEGIN -->
    <span id="tradingview-copyright"><a ref="nofollow noopener" target="_blank" href="http://www.tradingview.com" style="color: rgb(173, 174, 176); font-family: &quot;Trebuchet MS&quot;,Tahoma,Arial,sans-serif; font-size: 13px;">Forex Heat Map by <span style="color: #3BB3E4">TradingView</span></a></span>
    <script src="https://s3.tradingview.com/external-embedding/embed-widget-forex-heat-map.js">{
      "currencies": [
        "EUR",
        "USD",
        "JPY",
        "GBP",
        "INR"
      ],
      "width": "450",
      "height": "500",
      "locale": "en"
    }</script>
<!-- TradingView Widget END -->

The script part is usually suppressed by Wordpress. Please let me know if I can directly add widgets on Wordpress page. If by hooking in function.php if it could be done a sample code would be very useful. My given code works fine in plain html.

</div>
  • 写回答

1条回答 默认 最新

  • dtv55860 2017-09-14 04:01
    关注

    If you just want to insert that script into a page, you could use a plugin or set up a custom field in ACF, but the easiest way is to create a shortcode that you can add into the post editor.

    Create a function in functions.php to display the script, and then use add_shortcode to define the shortcode to use. e.g.:

    /* function that just displays the script */
    function insert_tradingview_heatmap_shortcode() { ?>
        <!-- TradingView Widget BEGIN -->
        <span id="tradingview-copyright"><a ref="nofollow noopener" target="_blank" href="http://www.tradingview.com" style="color: rgb(173, 174, 176); font-family: &quot;Trebuchet MS&quot;,Tahoma,Arial,sans-serif; font-size: 13px;">Forex Heat Map by <span style="color: #3BB3E4">TradingView</span></a></span>
        <script src="https://s3.tradingview.com/external-embedding/embed-widget-forex-heat-map.js">{
          "currencies": [
            "EUR",
            "USD",
            "JPY",
            "GBP",
            "INR"
          ],
          "width": "450",
          "height": "500",
          "locale": "en"
        }</script>
    <!-- TradingView Widget END -->
    <?php
    }
    /* create a shortcode called tradingview_heatmap that will run the function */
    add_shortcode('tradingview_heatmap', 'insert_tradingview_heatmap_shortcode');
    

    Then to display the heat map in a post/page, your just need to put the following shortcode into the post editor:

    [tradingview_heatmap]
    

    UPDATE:

    It might help to get a very simple shortcode to work first, so we can rule out anything with it.

    Add this to your functions.php:

    /* function to display a test message */
    function my_test_shortcode() { ?>
        <p>This is added by my test shortcode!</p>
    <?php
    }
    add_shortcode('my_test_shortcode', 'my_test_shortcode');
    

    Type the following into the post editor for a new, empty post, save it and view the post in your browser:

    [my_test_shortcode]
    

    It should print "This is added by my test shortcode!" as the post text.

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

报告相同问题?

悬赏问题

  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?