Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I´m trying to create a function that creates a breadcrumb trail of Page slugs outside of The Loop. These breadcrumbs show the path from the homepage to the current (sub)page.

For example, on the page "example.com/attractions/parcs/parc-name" this breadcrumb would be shown: Home > Attractions > Parcs > Parc Name

I´ve done a lot of research and found various code snippets that can perform part of the function, but my PHP skills are not good enough to create the whole function myself.

This is what I have:

  • Get the post slug in The Loop: $slug = basename(get_permalink()); (src)
  • Get the post slug outside of The Loop: global $post; echo $post->post_name; (src: see above)
  • Get posts´parent slug <?php global $post; if($post->post_parent) { $post_data = get_post($post->post_parent); echo $post_data->post_name; } ?> (src)
  • Get breadcrumb trail of pages using page titles (this is what I´m using now):

    if ( is_page() ) 
    {
      $post = $wp_query->get_queried_object();
      if ( $post->post_parent == 0 )
      { 
        echo "<li> &raquo; ".the_title('','', FALSE)."</li>"; 
      } 
      else 
      {
        $title = the_title('','', FALSE);
        $ancestors = array_reverse( get_post_ancestors( $post->ID ) );
        array_push($ancestors, $post->ID);
        foreach ( $ancestors as $ancestor )
        {
          if( $ancestor != end($ancestors) )
          {
            echo '<li> &raquo; <a href="'. get_permalink($ancestor) .'">'. strip_tags( apply_filters( 'single_post_title', get_the_title( $ancestor ) ) ) .'</a></li>';
          } 
          else 
          {
            echo '<li> &raquo; '. strip_tags( apply_filters( 'single_post_title', get_the_title( $ancestor ) ) ) .'</li>';
          }
        }
      }
    }
    

All ideas, suggestions and solutions are very much appreciated :)

share|improve this question
add comment

1 Answer

up vote 1 down vote accepted

Ok, problem solved. I´ll add the script below. Hope it´s useful for someone.

if ( is_page() ) {
            $post = $wp_query->get_queried_object();
            if ( $post->post_parent == 0 ){ echo "<li> &raquo; ".ucwords(str_replace("-", " ", $post->post_name))."</li>"; } 
            else {
                $title = the_title('','', FALSE);
                $ancestors = array_reverse( get_post_ancestors( $post->ID ) );
                array_push($ancestors, $post->ID);

                foreach ( $ancestors as $ancestor ){
                    if( $ancestor != end($ancestors) ){
                        echo '<li> &raquo; <a href="'. get_permalink($ancestor) .'">'. ucwords(str_replace("-", " ", basename(get_permalink($ancestor)))) .'</a></li>';
                    } else {
                        echo '<li> &raquo; '. ucwords(str_replace("-", " ", basename(get_permalink($ancestor)))) .'</li>';
                    }
                }
            }
share|improve this answer
    
I can´t accept my own answer yet :/ ..but it´s solved –  Mattvic Aug 4 '11 at 19:53
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.