2017-08-03 66 views
1

我已經在WordPress 4.8版本中創建了小部件區域,在該小部件區域中,我正在使用文本小部件。無法運行wordpress自定義小部件區域中的代碼

代碼:

<div id='div-gpt-ad-xxxxxxxxxxxxx-x'> 
<script> 
googletag.cmd.push(function() { googletag.display('div-gpt-ad-xxxxxxxxxxxxx-x'); }); 
</script> 
</div> 

控件創建代碼:

global $blog_id; 
if($blog_id == '2') { 

    function wpb_widgets_init() { 

    register_sidebar(array(
     'name'   => 'Header Widget Area for RBI', 
     'id'   => 'custom-header-widget', 
     'before_widget' => '<div class="chw-widget">', 
     'after_widget' => '</div>', 
     'before_title' => '<h2 class="chw-title">', 
     'after_title' => '</h2>', 
    )); 

} 
add_action('widgets_init', 'wpb_widgets_init'); 

} 
<?php 
global $blog_id; 
if($blog_id == '2') { 

if (is_active_sidebar('custom-header-widget')) : ?> 
    <div id="header-widget-area" class="chw-widget-area widget-area" role="complementary"> 
    <?php dynamic_sidebar('custom-header-widget'); ?> 
    </div> 

<?php endif; } ?> 

如果我們直接將在header.php文件或文件footer.php它工作正常,但在文本組件不是上述腳本工作任何解決方案

回答

0

您可以使用簡碼完成此操作。

首先,啓用的functions.php與以下行小部件簡碼:

add_filter('widget_text', 'do_shortcode'); 

接下來,創建一個短代碼:

function mygooglead_shortcode() { 
    ob_start(); 
    ?> 
<div id='div-gpt-ad-xxxxxxxxxxxxx-x'> 
<script> 
googletag.cmd.push(function() { googletag.display('div-gpt-ad-xxxxxxxxxxxxx-x'); }); 
</script> 
</div> 
    <?php 
    return ob_get_clean(); 
} 
add_shortcode('mygooglead', 'mygooglead_shortcode'); 

最後。你可以通過把它放在它的小部件中的短代碼:

[mygooglead] 
0

您通常不能將PHP直接放入小部件,Wordpress不允許它。有幾個插件可以讓你做到這一點,但我不能擔保任何人,所以我會讓你自己找到它。

+0

以上是不是php腳本它是js腳本與谷歌廣告有關 – krishna

相關問題