2016-11-23 56 views
0

我正在使用WordPress,我正在使用文本小部件。我正在使用這一行代碼,它工作得很好。如何獲得widget值到任何WordPress模板的源代碼

<?php if (is_active_sidebar('sidebar-1')) : ?> 
    <div class="rightside" role="complementary"> 

    <?php dynamic_sidebar('sidebar-1'); ?> 

    </div> 
<?php endif; ?> 

其實我的要求我只是想小部件價值爲源代碼一樣

<?php if (is_active_sidebar('sidebar-1')) : ?> 
      <div class="rightside" role="complementary"> 

      <?php 
         **$x= dynamic_sidebar('sidebar-1'); 

         echo $x;** 
       ?> 
      </div> 

    <?php endif; ?> 

我怎樣才能得到widget的所有內容到源代碼

回答

0

您可以在下面使用功能(粘貼在你的functions.php中):

if(!function_exists('get_dynamic_sidebar') { 

    function get_dynamic_sidebar($index = 1) { 
    $sidebar_contents = ""; 
    ob_start(); 
    dynamic_sidebar($index); 
    $sidebar_contents = ob_get_clean(); 

    return $sidebar_contents; 
    } 
} 

所以你的代碼看起來像這樣:

<?php if (is_active_sidebar('sidebar-1')) : ?> 
      <div class="rightside" role="complementary"> 

      <?php 
         $x= get_dynamic_sidebar('sidebar-1'); 

         echo $x; 
       ?> 
      </div> 

    <?php endif; ?> 
相關問題