检索Wordpress短代码



我希望检索页面上的短代码的对象(或数组)。所以基本上我有一个短代码,我的用户可以插入锚到一个页面,我正在寻找动态输出基于页面上的短代码菜单。有什么方法可以检索用户在页面渲染时输入的内容吗?

我是这样把文章的一部分放到侧边栏的:

在header.php中定义一个全局变量来保存信息:

// Initialize sidebar content.
$GLOBALS['mySidebar'] = '';

短代码的每个实现都应该将信息添加到该全局变量中。我的短代码只是为短代码内容添加HTML,您应该捕获短代码的含义:

add_shortcode('sidebar', 'addToSidebar');
// Add content to the sidebar.
function addToSidebar($attributes, $content) {
    $GLOBALS['mySidebar'] .= do_shortcode($content);
    return '';
}

呈现页面元素,使用全局变量:

<?php if (have_posts()) :
    the_post();
    // Get content first to retrieve the sidebar.
    $content = get_the_content();
    $content = apply_filters('the_content', $content);
    $content = str_replace(']]>', ']]&gt;', $content); ?>
    <?php if (!empty($GLOBALS['mySidebar'])) :?>
        <div id="sidebar"> <?php echo $GLOBALS['mySidebar']; ?> </div>
    <?php endif; ?>
    <div id="content">
    ... render the post.
<?php endif; ?>

最新更新