【问题标题】:How to get HTML of a sidebar content in wordpress?如何在 wordpress 中获取侧边栏内容的 HTML?
【发布时间】:2015-08-24 00:57:41
【问题描述】:

我可以通过wp_registered_sidebars获取侧边栏列表

<?php foreach ( $GLOBALS['wp_registered_sidebars'] as $sidebar ) { ?>
     <input value="<?php echo ucwords( $sidebar['id'] ); ?>">
              <?php echo ucwords( $sidebar['name'] ); ?>
<?php } ?>

我可以输出侧边栏

<?php echo dynamic_sidebar( ucwords( $sidebar['id'] )); ?>

但我需要输出 HTML 内容。

我正在尝试将侧边栏插入菜单 (megamenu),我正在关注 this post 如何让元框出现在我的菜单页面中。我已经完成了这一切,我可以获得侧边栏的名称、ID,一切。只是当我尝试在菜单的描述字段中添加 HTML 内容时,我不知道如何。如果我把 dynamic_sidebar 我会在我的菜单页面中得到实际的侧边栏,这不是我想要的。

感谢您的帮助。

ob_start()函数可以做到吗?

编辑

我试过了:

<?php
    ob_start();
    dynamic_sidebar($sidebar['id']);
    $sidebar_html = ob_get_contents();
    ob_end_clean();
?>

但是当我在隐藏输入字段中回显该变量时

<input type="hidden" class="menu-item-description" name="menu-item[-1][menu-item-description]" value="<?php echo $sidebar_html; ?>">

我仍然把 html 拿出来。我只需要它作为不会被“执行”的文本 html。

【问题讨论】:

    标签: html wordpress menu


    【解决方案1】:

    好的,htmlentities() 做到了

    <input type="hidden" class="menu-item-description" name="menu-item[-1][menu-item-description]" value="<?php echo htmlentities($sidebar_html); ?>">
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-02
      • 1970-01-01
      • 2012-07-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多