Forums

How to add subpages to static Pages template (2 posts)

  1. Amy
    Member
    Posted 1 year ago #

    Hi all. I came into a snag that I've been trying to fix. I've searched the Internet to find the solution to existing code which I used on the WordPress site. Am using static pages navigation code, not dynamic code, that allows current page highlighting. All I want to do is add sub/child pages to the existing code that already has parent pages set up and work well so far. I'm not familiar with conditional tags nor did I find an answer to. I'd like to use background images rather than text for subpages. Here's the code I'm using below.

    <ul>
    
    <li id="home_on" class="page_item <?php if(is_home(' ')) echo ' current_page_item'; ?>"><a href="home" class="home">Home<span></span></a></li>
    
    <li id="about_on" class="page_item <?php if(is_page('about')) echo ' current_page_item'; ?>"><a href="about" class="about">About<span></span></a>
    
    <li id="members_on" class="page_item <?php if(is_page('members')) echo ' current_page_item'; ?>"><a href="members" class="members">Members<span></span></a></li>
    
    <li id="donate_on" class="page_item <?php if(is_page('donate')) echo ' current_page_item'; ?>"><a href="donate" class="donate">Donate<span></span></a></li>
    
    <li id="contact_on" class="page_item <?php if(is_page('contact-us')) echo ' current_page_item'; ?>"><a href="contact-us" class="contact">Contact<span></span></a></li>
    </ul>

    I will not post the CSS that styles this navigation menu but let me know if it's helpful. I hope someone would chime in and give me a pointer how to add sub/child pages to the parent pages based on the code above or if there's a better way to do this without resorting to dynamic navigation menu or wp list page code.

  2. Jeremy Boggs
    Member
    Posted 1 year ago #

    It isn't quite clear from your code example how exactly you want to add the child pages. You can get the child pages using get_pages() function, and loop through each of those pages. There is an example on that Codex page dealing with child pages, which you should be able to modify to suit your needs. Something like:

    <?php $pages = get_pages('child_of=8'); ?>

    From there, you can loop through those pages, displaying them as list items or however you'd like to on your page template. If you'd just like a list of links to subpages, you could do:

    <?php
    
    $pages = get_pages('child_of=8');
    
    foreach ($pages as $page) {
        echo '<a href="' . get_page_link($page->ID) .'">'.$page->post_title .'</a>';
    }

    There are plenty of parameters for get_pages(), which are all described on the Codex page.

Topic Closed

This topic has been closed to new replies.

About this Topic