WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] How to display parent pages up to a certain level (5 posts)

  1. Marventus
    Member
    Posted 3 years ago #

    Hello everybody.

    My problem seems relatively simple, yet I can't seem to find a simple solution for it.

    I have a series of pages organized in 4 Levels, like so:
    Level 1
    - Level 2
    - - Level 3
    - - - Level 4

    What I would like to do, is add a sort of breadcrumb right before each page title which would display all parent pages (as links) of that current page in ascending order, but only up to Level 2 included.
    Here are some examples:
    @ If I'm on a Level 4 page, it would read:
    Level 2 Page title | Level 3 Page title | Level 4 page title
    @ If I'm on a Level 3 page, it would read:
    Level 2 Page title | Level 3 Page title
    @ If I'm on a Level 2 page, it would read:
    Level 2 Page title

    I tried using wp_list_pages but I couldn't get it to work because it does not have a parent parameter. I also tried using get_pages('parent=0&hierarchical=0'), but I couldn't get WP to display the pages obtained.

    Any help on this will be greatly appreciated!

  2. chinmoy
    Member
    Posted 3 years ago #

  3. Marventus
    Member
    Posted 3 years ago #

    Hi chinmoy,

    I was hoping to do it without plugins, since AFAIK it should pretty straight-forward. It's just that, for some reason, I can't seem to make it work.

  4. Marventus
    Member
    Posted 3 years ago #

    Hello again.
    I found a breadcrumb function on this site that comes really close to what I want. The code works for categories, pages, tags, etc., but since I only need this functionality for pages, I stripped down the rest and here's what I got:

    function wordpress_breadcrumbs() {
      $delimiter = '|';
      $currentBefore = '<span class="current">';
      $currentAfter = '</span>';
      if ( !is_home() && !is_front_page() || is_paged() ) {
        echo '<div id="crumbs">';
        global $post;
    	if ( is_page() && !$post->post_parent ) {
    		echo $currentBefore;
    		the_title();
    		echo $currentAfter; }
    	elseif ( is_page() && $post->post_parent ) {
          $parent_id  = $post->post_parent;
          $breadcrumbs = array();
          while ($parent_id) {
            $page = get_page($parent_id);
            $breadcrumbs[] = '<a href="' . get_permalink($page->ID) . '">' . get_the_title($page->ID) . '</a>';
            $parent_id  = $page->post_parent;
          }
          $breadcrumbs = array_reverse($breadcrumbs);
          foreach ($breadcrumbs as $crumb) echo $crumb . ' ' . $delimiter . ' ';
          echo $currentBefore;
          the_title();
          echo $currentAfter;
        }
        echo '</div>';
      }
    }

    This is working well but it is not hiding page Level 1. Is there a way to add a filter or exclude parameter to this code so it does?

  5. jbisenius
    Member
    Posted 2 years ago #

    Marventus, you're an absolute star! Thank you so much for sharing this, been trying to get my head around this for AGES! Thank you!!

    For the not so fluent user, I would only add the following. The code Marventus pasted would go into the functions.php file, and where you want the code to appear, you would insert
    <?php wordpress_breadcrumbs(); ?>

    Thanks again!!

Topic Closed

This topic has been closed to new replies.

About this Topic