John the Painter
John the Painter

Reputation: 2615

Wordpress - List child pages but link them as an anchor

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

I have this page template, which you can view here: http://goo.gl/y088X. 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'); ?>

Upvotes: 0

Views: 2162

Answers (1)

Marty
Marty

Reputation: 4657

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

http://codex.wordpress.org/Function_Reference/get_pages

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

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

hopefully that helps?

Marty

Upvotes: 4

Related Questions