Support » Plugins » Fix breadcrumb code to work with custom post types

  • Resolved jonathantimar

    (@jonathantimar)


    For a long time I have been using the following code to produce breadcrumbs in my theme. I use it because I do not like being dependent on plugins for basic functions.

    // Breadcrumbs
      function limelight_breadcrumbs()
      {
          $delimiter = ' > ';
          //text for the 'Home' link
          $name = __("Home", "limelight");
          $currentBefore = ' <span class="current">';
          $currentAfter = '</span> ';
          if (!is_home() && !is_front_page() || is_paged()) {
              echo '<nav id="breadcrumbs">';
              global $post;
              $home = get_bloginfo('url');
              echo '<a href="' . $home . '">' . $name . '</a> ' . $delimiter . '';
    	  if (is_category()) {
                  global $wp_query;
                  $cat_obj = $wp_query->get_queried_object();
                  $thisCat = $cat_obj->term_id;
                  $thisCat = get_category($thisCat);
                  $parentCat = get_category($thisCat->parent);
                  if ($thisCat->parent != 0)
                      echo(get_category_parents($parentCat, true, '' . $delimiter . ''));
                  echo $currentBefore . single_cat_title() . $currentAfter;
              } elseif (is_day()) {
                  echo '<a href="' . get_year_link(get_the_time('Y')) . '">' . get_the_time('Y') . '</a> ' . $delimiter . '';
                  echo '<a href="' . get_month_link(get_the_time('Y'), get_the_time('m')) . '">' . get_the_time('F') . '</a> ' . $delimiter . ' ';
                  echo $currentBefore . get_the_time('d') . $currentAfter;
              } elseif (is_month()) {
                  echo '<a href="' . get_year_link(get_the_time('Y')) . '">' . get_the_time('Y') . '</a> ' . $delimiter . '';
                  echo $currentBefore . get_the_time('F') . $currentAfter;
              } elseif (is_year()) {
                  echo $currentBefore . get_the_time('Y') . $currentAfter;
              } elseif (is_attachment()) {
                  echo $currentBefore;
                  the_title();
                  $currentAfter;
              } elseif (get_post_type()) {
                  $term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) );
            echo  $currentBefore . $term->name . $currentAfter;
    
              } elseif (is_single()) {
                  $cat = get_the_category();
                  $cat = $cat[0];
                  echo get_category_parents($cat, true, ' ' . $delimiter . '');
                  echo $currentBefore;
                  the_title();
                  echo $currentAfter;
              } elseif (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;
              } elseif (is_search()) {
                  echo $currentBefore . get_search_query() . $currentAfter;
              } elseif (is_tag()) {
                  echo $currentBefore . single_tag_title() . $currentAfter;
              } elseif (is_author()) {
                  global $author;
                  $userdata = get_userdata($author);
                  echo $currentBefore . $userdata->display_name . $currentAfter;
              } elseif (is_404()) {
                  echo $currentBefore . '404 Not Found' . $currentAfter;
              }
              if (get_query_var('paged')) {
                  if (is_home() || is_category() || is_day() || is_month() || is_year() || is_search() || is_tag() || is_author())
                      echo  $currentBefore;
                  echo __('Page','limelight') . ' ' . get_query_var('paged');
                  if (is_home() || is_category() || is_day() || is_month() || is_year() || is_search() || is_tag() || is_author())
                      echo $currentAfter;
              }
              echo '</nav>';
          }
      }

    However I now have need for custom post types, and this function can’t cope with them. I have searched high and low, and nowhere can I find a complete answer as to how to fix this code to work with custom post types. Most replies are non helpful “use such and such plugin” variety.

    I have dug through a few breadcrumb plugins that do work with custom post types in hopes of discovering how to apply it to my own code, but to no avail.

    I would be greatly appreciative if someone modified my above code to work with custom post types. I know it must be fairly simple, but with my very rudimentary knowledge of PHP I just can’t figure it out.

Viewing 7 replies - 1 through 7 (of 7 total)
  • No one has any input on this at all?

    Please?

    (if I’m wrong, Please correct)

    You have to add in the custom post type name. For each CPT.
    You’d change your code to

    elseif ( get_post_type() == 'Post Type Name' ) {
    echo $delimiter;
    echo get_the_term_list($post->ID, 'Custom Taxonomy Name', ' ', ', ', ' ' . $delimiter . ' ');
          the_title();      
    
    		}

    Where Post Type Name you’d put in the custom post type as you registered it in functions.php. So if you registered a cpt like garden-tools that’s what you’d put. Same goes for the custom taxonomy.
    Anyway, I am probably wrong but that is how I got my breadcrumbs working again.

    Thanks I have seen a version of this code, and while I am sure it would work, it’s a bit clumsy. It requires manual input of each custom post type, not practical in my case.

    There must be a way to make the code more “universal”. Certainly the majority of the breadcrumb plugins are able to do this, but I refuse to be reliant on a plugin. I have tried to examine the code of these plugins and adapt it to my function above, but alas it’s beyond my extremely basic PHP skills.

    Of course code that detects custom posts types and taxonomies is better so I found this code by Bill Erickson:wordpress-taxonomy-breadcrumbs and tried it out. I did not include it in functions.php as a separate function – I just added it into my existing breadcrumb code, it works but you’ll have to tweak it to get your delimiter character.

    Ah, I wrote too fast. That the code detects custom taxomomies – the post type itself is not honored… so the breadcrumb will crap out if you go to the single post type. I looked first at the archive for the post type – that is why I thought it worked. Sorry, thought I had a good solution there for you for a minute.

    oh well, thanks for trying, mccormicky, I do appreciate it!

    [ Please do not bump, that’s not permitted here. ]

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Fix breadcrumb code to work with custom post types’ is closed to new replies.