单页导航菜单动态生成



大家好!我已经在WP支持论坛上发布了这个问题,但是社区似乎不像stack那样活跃,所以我在这里碰碰运气!

我正在寻找一个插件,将自动创建一个导航菜单(通过使用短代码为例)在一个长单页文档页面。这一长页被分成几个部分。我可以想象在每个部分的开头使用一个短代码,这将创建一个菜单,例如,它将显示在侧边栏中(可能通过第二个短代码或小部件调用)

任何想法吗?建议吗?

谢谢!

使用[section]Section Title[/section]短代码,然后在需要输出导航链接的地方使用[section_navigation]

这是有效的,但有一个大量的警告- [section_navigation]需要在你的帖子/页面之后的其他[section]短代码…否则它将生成一个空列表。

你应该可以在你的主题中使用它,把<?php echo do_shortcode("[section_navigation]");?>放在sidebar.php。只要get_sidebar()在主题模板中的the_content()之后(通常是),它就可以工作。

这要放到functions.php

$whit_sections = "";
// [section]My Section Title[/section]                                          
function whit_section_shortcode( $atts, $title = null ) {
    // $content is the title you have between your [section] and [/section] 
    $id = urlencode(strip_tags($title)); 
    // strip_tags removes any formatting (like <em> etc) from the title.
    // Then urlencode replaces spaces and so on.
    global $whit_sections;
    $whit_sections .= '<li><a href="#'.$id.'">'.$title.'</a></li>';
    return '<span id="'.$id.'">'.$title.'</span>';
}
add_shortcode('section', 'whit_section_shortcode');

// [section_navigation]
function whit_section_navigation_shortcode( $atts, $title = null ) {
    global $whit_sections;
    return '<ul class="section-navigation">'.$whit_sections.'</ul>';
}
add_shortcode('section_navigation', 'whit_section_navigation_shortcode');

最新更新