0

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

4

1 回答 1

0

这是我为了能够将部分帖子放在侧边栏中所做的事情:

在 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; ?>
于 2011-08-25T21:56:09.677 回答