2012-06-21 22:53

Wordpress - 列出子页面,但将它们链接为锚点


Bit of a tough one to explain here, but I'll do my best.

I have this page template, which you can view here: The template firstly lists all the pages that are children of the category 'Exhibitions Archive' at the top as a sub menu, then the page outputs the attachments of these pages (images) on the page and uses some JS for presentation etc.

Currently, the submenu (the list of the child pages) items link to the pages, which is the obvious thing to do.

However, what I'd like to be able to do is keep everything on one page and have the sub menu items link to the post on the page and to scroll down to that section on the page (essentially, using anchor links). If you clicked on a exhibition title link in the sub menu, it would scroll to the title of the exhibition further down the page.

I have most of it set up, but I need to be able to do this dynamically. The titles down the pages marking the start of each section would need be be an anchor tag with the id tag referencing the sub-menu item, so when the sub menu item is clicked it'll scroll to that location with the sub menu item having the #anchor as the href, rather than a permalink to that page.

Is this at all possible?

This is what I'm using to list the items in the sub-menu, so I need a way of it dynamically creating a #anchor as the href rather than the permalink, and then the <h1> title down the pages marking each section to have the corresponding id.

Hope this makes sense, and hope you can help. I'll give some mega reputation for anyone who works this out :)

Thanks in advance, R

<?php wp_list_pages('title_li=&child_of=155'); ?>
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答


  • dongwei2983 dongwei2983 9年前

    instead of using wp_list_page() you could try writing your own sub nav using get_pages()

    once you build out your nav you can create and link to your content further down the page anyway you want..

    if( is_page(155) ){
        $args = array( 'child_of' => 155, 
                       'sort_order' => ASC,
                       'sort_column' => post_date,
                       'parent' => 155,
                       'hierarchical' => 0  
        $mypages = get_pages($args);
        <?php foreach( $mypages as $page ) {    
       <li><a href="#<?php echo $page->post_name; ?>"><?php echo $page->post_title; ?></a></li>

    hopefully that helps?


    点赞 评论 复制链接分享