2016-11-23 60 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; ?> 
相关问题