问题描述:

WordPress的页面

嵌入Tradingview插件我的WordPress的一个非常初级。我需要在我的Wordpress页面上添加一个交易视图小部件。代码如下。

<!-- TradingView Widget BEGIN --> 
 
\t <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> 
 
\t <script src="https://s3.tradingview.com/external-embedding/embed-widget-forex-heat-map.js">{ 
 
\t "currencies": [ 
 
\t  "EUR", 
 
\t  "USD", 
 
\t  "JPY", 
 
\t  "GBP", 
 
\t  "INR" 
 
\t ], 
 
\t "width": "450", 
 
\t "height": "500", 
 
\t "locale": "en" 
 
\t }</script> 
 
<!-- TradingView Widget END -->

该脚本部分通常由WordPress抑制。请让我知道我是否可以直接在Wordpress页面上添加小部件。如果通过挂接在function.php中,如果可以完成,示例代码将非常有用。我给出的代码在纯html中工作正常。

如果您只是想将该脚本插入到页面中,可以使用插件或在ACF中设置自定义字段,但最简单的方法是创建一个短代码,您可以将其添加到帖子编辑器中。

functions.php中创建函数以显示该脚本,然后使用add_shortcode来定义要使用的简码。例如:

/* 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'); 

然后显示在后/页的热图,你只需要把以下简码进入后编辑:

[tradingview_heatmap] 

UPDATE:

它可能有助于获得一个非常简单的简码,因此我们可以排除任何事情。

添加到您的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'); 

键入以下为文章编辑器一个新的空后,将其保存并查看这篇文章在浏览器中:

[my_test_shortcode] 

它应该打印“这是我的测试短代码添加!”作为文章文本。

+0

感谢您的回答,但我想我失去了一些东西。我已经注册使用下面的function.php – rajesh

+0

代码@rajesh我测试的代码和短代码之前我张贴和它完美的功能。你是否将代码直接放入了functions.php中?没有必要注册任何东西。此外,您是否使用标准的帖子编辑器在新的空白帖子或页面中测试短代码(即不是自定义帖子类型)?或者你想使用PHP添加它? – FluffyKitten

+0

感谢您的回答。我已经注册了function.php使用下面的cd(我的主题) 功能tv_scripts(){ wp_enqueue_script功能( '外汇',get_stylesheet_directory_uri() '/js/tview.js'); } ADD_ACTION( 'wp_enqueue_scripts', 'tv_scripts'); 我加入的js文件夹下的新的js文件tview.js给定的代码,不给错误。但是当我尝试将shortcode [tradingview_heatmap]添加到编辑器的文本窗口时,它不会给出结果。如何把它放在帖子编辑器? (我想我问的是愚蠢的问题)我已经尝试了一些后期编辑器插件,包括TinyMCE。不知道如何使用它。 – rajesh