Skip to main content
replaced http://it1.php.net with https://www.php.net
Source Link

Doing same thing in more concise way:

function breadcrump_page( $post ) {
  $format = '<a href="%s" title="%s">%s</a> &gt;';    
  $anc = array_map( 'get_post', array_reverse( (array) get_post_ancestors( $post ) ) );
  $links = array_map( 'get_permalink', $anc );
  foreach ( $anc as $i => $apost ) {
    $title = apply_filters( 'the_title', $apost->post_title );
    printf( $format, $links[$i], esc_attr($title), esc_html($title) );
  }
  echo apply_filters( 'the_title', $post->post_title );
}

global $post;
if ( $post->post_parent ) breadcrump_page( $post );

WordPress has a core function get_post_ancestors that retrieve all the parent ids for hierarchical post. I called it, reverse the order becuse they are returned form direct parent to highest ancestor, and finally get all post objects and all permalinks using a couple of array_maparray_map.

Doing that, is just matter of glue all the pieces and output them.

Doing same thing in more concise way:

function breadcrump_page( $post ) {
  $format = '<a href="%s" title="%s">%s</a> &gt;';    
  $anc = array_map( 'get_post', array_reverse( (array) get_post_ancestors( $post ) ) );
  $links = array_map( 'get_permalink', $anc );
  foreach ( $anc as $i => $apost ) {
    $title = apply_filters( 'the_title', $apost->post_title );
    printf( $format, $links[$i], esc_attr($title), esc_html($title) );
  }
  echo apply_filters( 'the_title', $post->post_title );
}

global $post;
if ( $post->post_parent ) breadcrump_page( $post );

WordPress has a core function get_post_ancestors that retrieve all the parent ids for hierarchical post. I called it, reverse the order becuse they are returned form direct parent to highest ancestor, and finally get all post objects and all permalinks using a couple of array_map.

Doing that, is just matter of glue all the pieces and output them.

Doing same thing in more concise way:

function breadcrump_page( $post ) {
  $format = '<a href="%s" title="%s">%s</a> &gt;';    
  $anc = array_map( 'get_post', array_reverse( (array) get_post_ancestors( $post ) ) );
  $links = array_map( 'get_permalink', $anc );
  foreach ( $anc as $i => $apost ) {
    $title = apply_filters( 'the_title', $apost->post_title );
    printf( $format, $links[$i], esc_attr($title), esc_html($title) );
  }
  echo apply_filters( 'the_title', $post->post_title );
}

global $post;
if ( $post->post_parent ) breadcrump_page( $post );

WordPress has a core function get_post_ancestors that retrieve all the parent ids for hierarchical post. I called it, reverse the order becuse they are returned form direct parent to highest ancestor, and finally get all post objects and all permalinks using a couple of array_map.

Doing that, is just matter of glue all the pieces and output them.

added 487 characters in body
Source Link
gmazzap
  • 46.3k
  • 6
  • 95
  • 147

Doing same thing in more concise way:

function breadcrump_page( $post ) {
  $format = '<a href="%s" title="%s">%s</a> &gt;';    
  $anc = array_map( 'get_post', array_reverse( (array) get_post_ancestors( $post ) ) );
  $links = array_map( 'get_permalink', $anc );
  foreach ( $anc as $i => $apost ) {
    $title = apply_filters( 'the_title', $apost->post_title );
    printf( $format, $links[$i], esc_attr($title), esc_html($title) );
  }
  echo apply_filters( 'the_title', $post->post_title );
}

global $post;
if ( $post->post_parent ) breadcrump_page( $post );

WordPress has a core function get_post_ancestors that retrieve all the parent ids for hierarchical post. I called it, reverse the order becuse they are returned form direct parent to highest ancestor, and finally get all post objects and all permalinks using a couple of array_map.

Doing that, is just matter of glue all the pieces and output them.

Doing same thing in more concise way:

function breadcrump_page( $post ) {
  $format = '<a href="%s" title="%s">%s</a> &gt;';    
  $anc = array_map( 'get_post', array_reverse( (array) get_post_ancestors( $post ) ) );
  $links = array_map( 'get_permalink', $anc );
  foreach ( $anc as $i => $apost ) {
    $title = apply_filters( 'the_title', $apost->post_title );
    printf( $format, $links[$i], esc_attr($title), esc_html($title) );
  }
  echo apply_filters( 'the_title', $post->post_title );
}

global $post;
if ( $post->post_parent ) breadcrump_page( $post );

Doing same thing in more concise way:

function breadcrump_page( $post ) {
  $format = '<a href="%s" title="%s">%s</a> &gt;';    
  $anc = array_map( 'get_post', array_reverse( (array) get_post_ancestors( $post ) ) );
  $links = array_map( 'get_permalink', $anc );
  foreach ( $anc as $i => $apost ) {
    $title = apply_filters( 'the_title', $apost->post_title );
    printf( $format, $links[$i], esc_attr($title), esc_html($title) );
  }
  echo apply_filters( 'the_title', $post->post_title );
}

global $post;
if ( $post->post_parent ) breadcrump_page( $post );

WordPress has a core function get_post_ancestors that retrieve all the parent ids for hierarchical post. I called it, reverse the order becuse they are returned form direct parent to highest ancestor, and finally get all post objects and all permalinks using a couple of array_map.

Doing that, is just matter of glue all the pieces and output them.

Source Link
gmazzap
  • 46.3k
  • 6
  • 95
  • 147

Doing same thing in more concise way:

function breadcrump_page( $post ) {
  $format = '<a href="%s" title="%s">%s</a> &gt;';    
  $anc = array_map( 'get_post', array_reverse( (array) get_post_ancestors( $post ) ) );
  $links = array_map( 'get_permalink', $anc );
  foreach ( $anc as $i => $apost ) {
    $title = apply_filters( 'the_title', $apost->post_title );
    printf( $format, $links[$i], esc_attr($title), esc_html($title) );
  }
  echo apply_filters( 'the_title', $post->post_title );
}

global $post;
if ( $post->post_parent ) breadcrump_page( $post );