PHP Developer

Monday, 1 February 2016

Create Shortcode to Show Bredcrum of Post or Page in Wordpress

 Create Shortcode to Show Bredcrum of Post or Page in Wordpress


This Code helps you to show Bredcrum or post and page where you want. And Shortcode is [page_bredcrum]

<?php
/************** Create Shortcode For Bredcrum START ****************/

function page_bredcrum_path() {
    $delimiter = '>';
  $currentBefore = '<span class="current">';
  $currentAfter = '</span>';
  if ( !is_home() && !is_front_page() || is_paged() ) {
    echo '<div id="crumbs">';
    echo '<a href="'.site_url().'">Home</a> '.$delimiter.' ';
    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>';
  }
}
add_shortcode('page_bredcrum', 'page_bredcrum_path');


/************** Create Shortcode For Bredcrum END ****************/
?>

No comments:

Post a Comment