2014-12-10 122 views
2

我有一个优质的WordPress主题,默认情况下有面包屑,但在帖子页面上不显示类别(和子类别)项目:主页>分类>子-category>帖子标题我想添加“类别”到我的WP面包屑

相反,它只是显示如下:首页>文章标题

这是处理面包屑的功能:

(我标志着我认为部分代码这个标志很重要,以节省您的时间:========== >>)

// BREADCRUMBS 
if (! function_exists('ishyoboy_get_breadcrumbs')) { 
    function ishyoboy_get_breadcrumbs() { 

     global $ish_options, $ish_woo_id; 

     $return = ''; 

     $return .= '<div class="ish-pb-breadcrumbs"><div><div>' . "\n"; 

     if (! is_front_page()) { 
      if (function_exists('is_woocommerce')) { 
       if (!is_woocommerce() && !is_woocommerce_page()){ 
        $return .= '<a class="ish-pb-breadcrumbs-home" href="'; 
        $return .= home_url(); 
        $return .= '">'; 
        $return .= '<span>' . __('Home', 'ishyoboy') . '</span>'; 
        $return .= "</a> &gt; "; 
       } 

      } 
      else{ 
       $return .= '<a class="ish-pb-breadcrumbs-home" href="'; 
       $return .= home_url(); 
       $return .= '">'; 
       $return .= '<span>' . __('Home', 'ishyoboy') . '</span>'; 
       $return .= "</a> &gt; "; 
      } 

     } 
     else { 
      $return .= '<span class="ish-pb-breadcrumbs-home">'; 
      $return .= '<span>' . __('Home', 'ishyoboy') . '</span>'; 
      $return .= "</span>"; 
     } 

     if (!is_front_page() && is_home()) { 
      global $page; 
      $return .= $page->post_title; 
     } 

     if (is_archive() && 'post' == get_post_type() && ! is_category() && (!function_exists('is_woocommerce') || !is_woocommerce()) ) { 
      $hpage = get_page(get_option('page_for_posts')); 
      if ('page' == get_option('show_on_front') && isset($hpage) && '' != $hpage){ 
       $return .= get_page_parents($hpage->ID, TRUE, ' &gt; ', FALSE); 
      } 
      if (is_day()) : 
       $return .= sprintf(__('Daily Archives: %s', 'ishyoboy'), '<span>' . get_the_date() . '</span>'); 
      elseif (is_month()) : 
       $return .= sprintf(__('Monthly Archives: %s', 'ishyoboy'), '<span>' . get_the_date(_x('F Y', 'monthly archives date format', 'ishyoboy')) . '</span>'); 
      elseif (is_year()) : 
       $return .= sprintf(__('Yearly Archives: %s', 'ishyoboy'), '<span>' . get_the_date(_x('Y', 'yearly archives date format', 'ishyoboy')) . '</span>'); 
      else : 
       $return .= __('Archives', 'ishyoboy'); 
      endif; 
     } 
     else if (is_archive() && 'post' != get_post_type() && (!function_exists('is_woocommerce') || !is_woocommerce()) ) { 

      $type = get_post_type(get_the_ID()); 

      $obj = get_post_type_object($type); 
      if (is_object($obj)){ 
       $return .= $obj->labels->name; 
      } 

     } 

     if ((is_category() || is_single()) && ((!function_exists('is_woocommerce_page') || !function_exists('is_woocommerce')) || (!is_woocommerce() && !is_woocommerce_page()))) { 

      $post_id = ish_get_the_ID(); 
      $post_type = get_post_type(); 

      switch ($post_type){ 
       case 'portfolio-post' : 
        $terms = get_the_terms($post_id, 'portfolio-category'); 
        $term = (! empty($terms)) ? array_pop($terms) : ''; 

        if (isset($ish_options['page_for_custom_post_type_portfolio-post']) && '-1' != $ish_options['page_for_custom_post_type_portfolio-post']){ 

         $portfolio_page = get_page($ish_options['page_for_custom_post_type_portfolio-post']); 
         $separator = " &gt; "; 
         $return .= '<a href="' . get_page_link($ish_options['page_for_custom_post_type_portfolio-post']) . '" title="' . esc_attr(sprintf(__("View all posts in %s" , 'ishyoboy'), $portfolio_page->post_title)) . '">'.$portfolio_page->post_title.'</a>' . $separator; 
        } 

        if (! empty($term)){ 
         $return .= get_term_parents($term->term_id, 'portfolio-category', TRUE, ' &gt; ', FALSE); 
        } 
        break; 

// from here ==========>> 

       case 'post' : 

        if (is_category()){ 
         global $cat; 
         $category = get_category($cat); 
         if ($category->parent && ($category->parent != $category->term_id)){ 
          $return .= get_category_parents($category->parent, TRUE, ' &gt; ', FALSE); 
         } 
         $return .= single_cat_title('', false); 
        } 
        else{ 
         $category = get_the_category(); 
         if (is_object($category)){ 
          $ID = $category[0]->cat_ID; 
          $return .= get_category_parents($ID, TRUE, ' &gt; ', FALSE); 
         } 
        } 
        break; 

// to here ==========>> 

       default : 
        $type = get_post_type(get_the_ID()); 

        $obj = get_post_type_object($type); 
        if (is_object($obj)){ 
         $return .= '<a href="' . get_post_type_archive_link($type) .'">' . $obj->labels->name . '</a> &gt; '; 
        } 

      } 
     } 
     else if ((function_exists('is_woocommerce_page') && function_exists('is_woocommerce')) && (is_woocommerce() || is_woocommerce_page())){ 
      ob_start(); 
      woocommerce_breadcrumb(array(
       'delimiter' => ' &gt; ', 
       'wrap_before' => '', 
       'wrap_after' => '', 
       'before'  => '', 
       'after'  => '', 
       'home'  => '<span class="ish-pb-breadcrumbs-home"><span>' . _x('Home', 'breadcrumb', 'woocommerce') . '</span></span>', 
      )); 
      $return .= ob_get_contents(); 
      ob_end_clean(); 
      ob_end_flush(); 
     } 
     else if (is_tax()){ 
      if (is_tax('portfolio-category')){ 

       $current_term = get_queried_object(); 

       if (!empty($current_term)){ 
        //var_dump($current_term); 

        if (isset($ish_options['page_for_custom_post_type_portfolio-post']) && '-1' != $ish_options['page_for_custom_post_type_portfolio-post']){ 

         $portfolio_page = get_page($ish_options['page_for_custom_post_type_portfolio-post']); 
         $separator = " &gt; "; 
         $return .= '<a href="' . get_page_link($ish_options['page_for_custom_post_type_portfolio-post']) . '" title="' . esc_attr(sprintf(__("View all posts in %s" , 'ishyoboy'), $portfolio_page->post_title)) . '">'.$portfolio_page->post_title.'</a>' . $separator; 
        } 

        if ($current_term->parent != 0){ 
         $return .= get_term_parents($current_term->parent, 'portfolio-category', TRUE, ' &gt; ', FALSE); 
        } 

        $return .= $current_term->name; 
       } 
      } 
     } 
     else if (is_page()){ 
      global $post; 

      if ($post->post_parent != 0){ 
       $return .= get_page_parents($post->post_parent, TRUE, ' &gt; ', FALSE); 
      } 
     } 

     if (!function_exists('is_woocommerce_page') || !is_woocommerce_page()){ 
      if(is_single()) {$return .= get_the_title();} 
      if(is_page()) { 
       global $post; 
       $frontpage = get_option('page_on_front'); 

       if ($frontpage && $frontpage == $post->ID){ 
        /*$return .= '<a class="home" href="'; 
        $return .= home_url(); 
        $return .= '">'; 
        $return .= '<span>' . __('Home', 'ishyoboy') . '</span>'; 
        $return .= "</a>";*/ 
       } else { 
        $return .= get_the_title(); 
       } 
      } 
      if(is_tag()){ $return .= __('Tag: ', 'ishyoboy') . single_tag_title('',FALSE); } 
      if(is_404()){ $return .= __('404 - Page not Found', 'ishyoboy'); } 
      if(is_search()){ $return .= __('Search', 'ishyoboy'); } 
      if(is_year()){ $return .= get_the_time('Y'); }; 
     } 

     $return .= '</div></div></div>'; 

     return $return; 
    } 
} 

if (!function_exists('the_post_thumbnail_caption')) { 
    function the_post_thumbnail_caption(){ 

     $thumb = get_post_thumbnail_id(); 

     if (! empty($thumb)){ 
      $thumb_object = get_post($thumb); 
      if (! empty($thumb_object)) { 
       echo '<div class="wp-caption"><p class="wp-caption-text">' . $thumb_object->post_excerpt . '</p></div>'; 
      } 
     } 

    } 
} 

if (!function_exists('get_the_post_thumbnail_caption')) { 
    function get_the_post_thumbnail_caption(){ 

     $thumb = get_post_thumbnail_id(); 

     if (! empty($thumb)) 
      return get_post($thumb)->post_excerpt; 

     return null; 
    } 
} 

if (! function_exists('ishyoboy_activate_fancybox_on_blog_single')) { 
    function ishyoboy_activate_fancybox_on_blog_single() { 

     if (is_singular() && !is_singular('product')){ 
      ?> 
      <script type="text/javascript"> 
       jQuery(document).ready(function($){ 
        var thumbnails = jQuery("a:has(img)").not(".nolightbox").filter(function() { return /\.(jpe?g|png|gif|bmp)$/i.test(jQuery(this).attr('href')) }); 

        if (thumbnails.length > 0){ 
         thumbnails.addClass('openfancybox-image').attr('rel', 'fancybox-post-image-<?php the_ID() ?>'); 
        } 
       }); 
      </script> 
     <?php 
     } 

    } 
} 
add_action('wp_head', 'ishyoboy_activate_fancybox_on_blog_single'); 


if (! function_exists('ishyoboy_dummy_functions')) { 
    function ishyoboy_dummy_functions() { 

     if (false) { 
      posts_nav_link(); 
      wp_link_pages(); 
      $args = ''; 
      add_theme_support('custom-header', $args); 
      add_theme_support('custom-background', $args); 
     } 

    } 
} 

然后我所做的代码产生所需变化的变化 - 类别出现在面包屑:首页>分类>子类别>帖子标题

但在分类存档页屑是这样的:首页>分类>类别

活生生的例子:http://pigtelligent.com/science/

改变我“VE在上述代码所做的:

 case 'post' : 
      $terms = get_the_terms($post_id, 'category'); 
      $term = (! empty($terms)) ? array_pop($terms) : ''; 
      if (! empty($term)){ 
       $return .= get_term_parents($term->term_id, 'category', TRUE, ' &gt; ', FALSE); 
      } 

我知道,有人更聪明,这应该是很容易的,但我花了一整天试图弄清楚这一点,所以你可以帮帮我吗? :)

回答

0

类别可能会变得棘手,因为帖子可以有多个类别,并且类别着陆页可以访问很多可能会引起误解的元。确保您已查看了代码的Template Hierarchy页面,并且您已经对WP处理分类的方式有了深入的了解。

使用What The File可以找出您实际使用的模板,因为您可能不在分类页面上,导致is_category()返回false。你可能在一个单一的(作为if陈述包装逻辑你指出检查is_category() || is_single()),或者你可能需要添加一个支票is_archive()

我最好的建议是弄清楚你是否在类别页面上,然后停止所有额外的逻辑,只是吐出类别的名称。没有太多的帮助,我知道,但我希望这会

+0

你是对的,我用的是什么文件,这的确是* is_archive() * 现在,您将如何解决“停止所有额外的逻辑并只是吐出类别名称”? – 2014-12-11 16:52:11

+0

太好了。如果(is_category()|| is_single())&& ...) 尝试: 'if(is_archive()){...} else if(is_category() )){...} else if(is_single()&& ...){}' – 2014-12-11 23:09:43

0
function the_breadcrumb() { 
    global $post; 
    echo '<ul id="breadcrumbs">'; 
    if (!is_home()) { 
     echo '<li><a href="'; 
     echo get_option('home'); 
     echo '">'; 
     echo 'Home'; 
     echo '</a></li><li class="separator">/</li>'; 
     if (is_category() || is_single()) { 
      echo '<li>'; 
      the_category(' </li><li class="separator">/</li><li> '); 
      if (is_single()) { 
       echo '</li><li class="separator">/</li><li>'; 
       the_title(); 
       echo '</li>'; 
      } 
     } elseif (is_page()) { 
      if($post->post_parent){ 
       $anc = get_post_ancestors($post->ID); 
       $title = get_the_title(); 
       foreach ($anc as $ancestor) { 
        $output = '<li><a href="'.get_permalink($ancestor).'" title="'.get_the_title($ancestor).'">'.get_the_title($ancestor).'</a></li> <li class="separator">/</li>'; 
       } 
       echo $output; 
       echo '<strong title="'.$title.'"> '.$title.'</strong>'; 
      } else { 
       echo '<li><strong> '.get_the_title().'</strong></li>'; 
      } 
     } 
    } 
    elseif (is_tag()) {single_tag_title();} 
    elseif (is_day()) {echo"<li>Archive for "; the_time('F jS, Y'); echo'</li>';} 
    elseif (is_month()) {echo"<li>Archive for "; the_time('F, Y'); echo'</li>';} 
    elseif (is_year()) {echo"<li>Archive for "; the_time('Y'); echo'</li>';} 
    elseif (is_author()) {echo"<li>Author Archive"; echo'</li>';} 
    elseif (isset($_GET['paged']) && !empty($_GET['paged'])) {echo "<li>Blog Archives"; echo'</li>';} 
    elseif (is_search()) {echo"<li>Search Results"; echo'</li>';} 
    echo '</ul>'; 
} 
?> 

开始使用你指出正确的方向:<?php the_breadcrumb(); ?>