• Hi, folks.

    What I want to do:
    I’ve recently built a website with a horizontal pulldown navigation. Basically my goal is to make the parent items a liquid width so that regardless of how many parent pages are present, the nav items will fill the horizontal bar (make sense so far?).

    What I need in order to accomplish this:
    What I need is to be able to count the number of parent pages only so that I can use that number to determine how wide in percentage to make each parent navigation item. I’ve accomplished this on another system (CMS Made Simple) but have yet to do so via WordPress.

    I found out how to count the total number of pages which is a step in the right direction. Now If I can exclude child pages from this count, I would be set. This is what I’ve got thus far:

    $num_pages = wp_count_posts('page');
    $num_pages = $num_pages->publish;
    echo $num_pages;

    If anyone has a much better way of accomplishing this, I’m open to that as well. Thanks in advance!

Viewing 4 replies - 1 through 4 (of 4 total)
  • Thread Starter Daryn St. Pierre

    (@bloqhead)

    I managed to find a solution to this which resulted in great success! Wooo! Big thanks to Troy Thompson for the tip and to Jen Rochester for RT’ing my original tweet. I’m surprised I overlooked this one. I had searched high and low but obviously not high nor low enough.

    Here’s the solution:
    This outputs the number of parent pages present.

    $pagecount = count(get_pages('parent=0'));
    echo $pagecount;

    Then I took it and added this in my header

    <style type="text/css">
    #nav li {
    	width: <?php
    	$pagecount = count(get_pages('parent=0'));
    	$navliwidth = 100 / $pagecount;
    	echo $navliwidth; ?>% !important;
    }
    </style>

    Voilá! fluid list items in my horizontal nav!

    Or

    $num_pages = wp_count_posts( 'page' );

    Thread Starter Daryn St. Pierre

    (@bloqhead)

    Michael,

    I have that in my first post but the problem I ran into was that it was counting all navigation items including the ones in drop down menus. So if it did the math for the width based on the amount of children pages AND parents, it would be inaccurate.

    I hope that makes sense. For example, if I have 5 parent navigation items and some of them have children, I want it to calculate the width of those top level items so that they fit dynamically.

    Ignore me–forgot you just wanted ‘parents’. Going back to sleep here ??

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Count Number of Parent Pages Only’ is closed to new replies.