WordPress.org

Ready to get started?Download WordPress

Forums

How to limit number of menu items shown (5 posts)

  1. JordashTalon
    Member
    Posted 5 years ago #

    I'm currently working on a new WordPress template,

    I list the pages in two places on the template.

    In one of those places i'd like to only list 5 pages instead of all of them.

    e.g. I need to do something like this
    <?php wp_list_pages('title_li=&sort_column=menu_order'); ?>

    And limit the amount listed to 5,

    Is that possible?

  2. tugbucket
    Member
    Posted 5 years ago #

    http://codex.wordpress.org/Template_Tags/wp_list_pages

    would it be possible for you to just exclude certain pages?

    <?php wp_list_pages('title_li=&sort_column=menu_order&exclude=3,7'); ?>

  3. JordashTalon
    Member
    Posted 5 years ago #

    That's not possible because I'm programming a theme that anyone should be able to use, and I wont know the page ids that need to be excluded/included, I want to list just the first five (to keep the layout from breaking).

    Thanks for the idea though.

  4. Technokinetics
    Member
    Posted 5 years ago #

    How about this approach:

    • Store the page list in a variable (<?php $page_list = wp_list_pages('title_li=&sort_column=menu_order&echo=0'); ?>)
    • Use explode() to split the page list into fragments, each one a list item
    • Reassemble the first five list items (or as many as there are if there are fewer) into a new page list
    • Output the new page list

    - Tim

  5. Milo
    Member
    Posted 5 years ago #

    Or via the theme functions file like so:

    function theme_nav_menu() {
    if (get_option('show_on_front') == 'page')
    $static_page_id = get_option('page_on_front');

    if ($static_page_id && is_page($static_page_id))
    $home_class = ' class="current_page_item"';
    else {
    if ($static_page_id && !is_page($static_page_id))
    $home_class = '';
    elseif (is_home())
    $home_class = ' class="current_page_item"';
    }

    $home_text = theme_home_link_text();

    echo '<ul id="tabs">' . "\n";
    echo '<li' . $home_class . '>< a rel=""nofollow"">' . $home . '' . "\n";

    if (theme_get_option('nav_menu_pages')) {
    $pages = &get_pages('sort_column=post_parent,menu_order');
    $nav_menu_pages = explode(',', theme_get_option('nav_menu_pages'));

    if ($pages && $nav_menu_pages) {
    foreach ($pages as $page) {
    if (!in_array($page->ID, $nav_menu_pages))
    $exclude_pages[] = $page->ID;
    }

    if ($exclude_pages)
    $exclude_pages = implode(',', $exclude_pages);

    wp_list_pages('title_li=&exclude=' . $static_page_id . ',' . $exclude_pages);
    }
    }

    then add a theme option panel described here and style it via CSS.

Topic Closed

This topic has been closed to new replies.

About this Topic