$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>functions - Slugs as breadcrumbs for Pages|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

functions - Slugs as breadcrumbs for Pages

matteradmin10PV0评论

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/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 :)

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/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 asked Aug 3, 2011 at 14:08 MattvicMattvic 9292 gold badges9 silver badges20 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 3

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>';
                    }
                }
            }
} // You just missed this bracket, else this is working awesome! 

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far