• I’ve been playing around with wp_list_pages in the codex section but it doesn’t make much sense to me yet.

    I just want to list all the page titles (as links) in a vertical column automatically so I don’t have to manually add a link every time I add a page. I feel like it has something to do with the title_li parameter but I can’t get it to do what I want.

    As a bonus, it would be nice to alternate background colors for each title to give the list some separation, but that might be asking too much…

    I’m sure it’s probably simpler than I’m making it, but I’m pulling my hair out. This is the first thing I’ve needed to do with PHP so I haven’t spent much time with it.

Viewing 4 replies - 1 through 4 (of 4 total)
  • Start with just a simple call to wp_list_pages(), with no parameters. That should give you the list.

    You can alternate the background color of rows with the CSS nth-child selector (but note that it does not work in older browsers). Here is a sample that will work if you have no child pages:

    li.pagenav ul li:nth-child(even) {background: #CCC}
    li.pagenav ul li:nth-child(odd) {background: #FFF}

    It gets a lot more complicated if there are multiple levels of children.

    Thread Starter bcjhood

    (@bcjhood)

    That looks much simpler than what I ended up using. I learned enough to do the following. I created a variable with value 0 then incremented it by one inside the while loop, using 2 different classes to alternate backgrounds.

    <div class = "<?php
      if ( $num % 2 == 0 ) : ?>
        titlecolor1
      <?php else : ?>
        titlecolor2
      <?php endif; $num++; ?>">
    
    <a href="<?php the_permalink() ?>"><?php the_title(); ?></a>
    
    </div>

    I don’t know what $num % 2 == 0 does but it works to find the even/odd value of $num.

    Actually, your solution is better than the one I gave because it does not depend on browser support for that level of CSS.

    The ‘%’ is called the ‘modulo’ operator. Basically, it says “What is the remainder of dividing the left argument by the right one?”

    So, when $num is even, $num % 2 will be zero. When $num is odd, $num % 2 will be one.

    Thread Starter bcjhood

    (@bcjhood)

    Ah, cool. Thanks for the help!

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Need help using PHP to list all my pages’ is closed to new replies.