SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    SitePoint Evangelist JordashTalon's Avatar
    Join Date
    Dec 2008
    Posts
    505
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Wordpress Question how to limit pages shown in the menu

    Sorry if this is in the wrong area, but the Wordpress Forums aren't very responsive:

    Does anyone know how to limit the amount of pages that show up in the menu in Wordpress:
    e.g.
    wp_list_pages("title_li=&sort_column=menu_order");

    and only list 5 pages in the menu instead of all of them?

    Thanks

  2. #2
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    17,161
    Mentioned
    190 Post(s)
    Tagged
    2 Thread(s)
    The wp_list_pages function is in the wp-includes/post-template.php file. It has these default arguments
    PHP Code:
        $defaults = array(
            
    'depth' => 0'show_date' => '',
            
    'date_format' => get_option('date_format'),
            
    'child_of' => 0'exclude' => '',
            
    'title_li' => __('Pages'), 'echo' => 1,
            
    'authors' => '''sort_column' => 'menu_order, post_title',
            
    'link_before' => '''link_after' => ''
        
    ); 
    Maybe if you supplied a depth parameter, eg.
    PHP Code:
    wp_list_pages("title_li=&sort_column=menu_order&depth=1"); 
    to not show sub-pages?
    Or you could explicitly exclude certain pages. http://codex.wordpress.org/Template_Tags/wp_list_pages

  3. #3
    SitePoint Evangelist JordashTalon's Avatar
    Join Date
    Dec 2008
    Posts
    505
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    the Depth attribute didn't work it still lists all the pages.

  4. #4
    @php.net Salathe's Avatar
    Join Date
    Dec 2004
    Location
    Edinburgh
    Posts
    1,397
    Mentioned
    63 Post(s)
    Tagged
    0 Thread(s)
    You could parse the HTML produced by wp_list_pages and extract only however many items you need.
    Salathe
    Software Developer and PHP Manual Author.

  5. #5
    SitePoint Evangelist JordashTalon's Avatar
    Join Date
    Dec 2008
    Posts
    505
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    that's a good idea, the only problem with that is whenever I call the wp_list_pages() it generates the HTML live. Even if I assign it to a variable.

    e.g. $pages=wp_list_pages();

    would generate the HTML and display it at the same time.

    hope that makes sense.

  6. #6
    SitePoint Enthusiast
    Join Date
    May 2006
    Posts
    75
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by JordashTalon View Post
    that's a good idea, the only problem with that is whenever I call the wp_list_pages() it generates the HTML live. Even if I assign it to a variable.

    e.g. $pages=wp_list_pages();

    would generate the HTML and display it at the same time.

    hope that makes sense.
    That's because wp_list_pages() function is set to "echo" by default. Take a look at this code at the bottom of the function:

    PHP Code:
    if ( $r['echo'] )
        echo 
    $output;
    else
        return 
    $output
    If you pass "echo=0" as additional argument, for example:

    PHP Code:
    wp_list_pages("title_li=&sort_column=menu_order&echo=0"); 
    this function will return result instead of displaying it on the browser.

  7. #7
    Is Still Alive silver trophybronze trophy RetroNetro's Avatar
    Join Date
    Jul 2003
    Location
    Texas
    Posts
    6,883
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Or you could explicitly exclude certain pages. http://codex.wordpress.org/Template_Tags/wp_list_pages
    Or you could use the "include" parameter to include only the 5 pages you want:
    Code PHP:
    wp_list_pages("title_li=&sort_column=menu_order&include=2,3,4,5,6");
    http://codex.wordpress.org/Template_..._Pages_in_List

  8. #8
    SitePoint Evangelist JordashTalon's Avatar
    Join Date
    Dec 2008
    Posts
    505
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hmm, yeah your right, that fixed the problem but caused another, I was able to parse the string and everything but for some reason using the echo 0 parameter caused all of the regular content items in my page only display one character, you can see it here:

    http://joomlahax.com/wordpress/

    I used this code:

    <?php
    $howmany = 4;

    $pages = wp_list_pages("title_li=&sort_column=menu_order&echo=0");

    $pages_arr = explode("\n", $pages);

    for($i=0;$i<$howmany;$i++){
    echo $pages_arr[$i];
    }

    ?>

    the pages that I want to limit are on the top, all the pages are on the left.

    Any ideas, or does anyone know an easier way?

    Thanks,

  9. #9
    Is Still Alive silver trophybronze trophy RetroNetro's Avatar
    Join Date
    Jul 2003
    Location
    Texas
    Posts
    6,883
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by JordashTalon View Post
    Any ideas, or does anyone know an easier way?

    Thanks,
    Yes use either the "include" or "exclude" parameters. If you know the 5 pages you want to include in the nav and these are never going to change, I'd go with the "include" parameter. In my example above include=2,3,4,5,6 are the page ID numbers. Hope it helps.

  10. #10
    SitePoint Evangelist JordashTalon's Avatar
    Join Date
    Dec 2008
    Posts
    505
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That would work if it was only me that was working on the site, however i'm making this with a Wordpress newbie in mind so at the beginning they'd only have one page and then they would add more and more so there is no way of knowing which page id's i'd need to exclude or include.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •