2011-04-27 73 views
1

我正在尋找一種方法來從我的麪包屑主題的get_category_parents中排除ID爲3和4的類別。這是代碼,有問題的線是11:從get_category_parents(wordpress)排除類別

function the_breadcrumb() { 
global $post; 
if (!is_home()) { 
    echo '<a href="'.get_option('home').'">'.home.'</a>'; 
    if (is_category()) { 
     echo "/"; 
     echo single_cat_title(); 
    } elseif(is_single() && !is_attachment()) { 
     $cat = get_the_category(); $cat = $cat[0]; 
     echo "/"; 
     echo get_category_parents($cat, TRUE, ' ' . $delimiter . ' '); 
     echo "/"; 
     echo thman_get_limited_string($post->post_title,30); 
    }  
    elseif (is_search()) { 
     echo "/" . cerca; 
    }  
    elseif (is_page() && $post->post_parent) { 
     echo '/<a href="'.get_permalink($post->post_parent).'">'; 
     echo get_the_title($post->post_parent); 
     echo "</a>/"; 
     echo thman_get_limited_string($post->post_title,30);   
    } 
    elseif (is_page() OR is_attachment()) { 
     echo "/"; 
     echo thman_get_limited_string($post->post_title,30); 
    } 
    elseif (is_author()) { 
     echo wp_title('/Profilo'); 
     echo ""; 
    } 
    elseif (is_404()) { 
     echo "/"; 
     echo errore_404; 
    }  
    elseif (is_archive()) { 
     echo wp_title('/');  
    } 
} 
    } 

回答

2
$cat = get_the_category(); 
$cat = $cat[0]->term_id; 
// next will return an array of all category ancestors, with toplevel cat being [0] 
$ancestors = array_reverse(get_ancestors($cat, 'category')); 
if($ancestors) { 
    // set up output 
    $output = ''; 
    foreach($ancestors as $cat) { 
    // skips cats 3 and 4 
    if($cat == '3' || $cat == '4') continue; 
    $catlink = get_category_link($cat); 
    $catname = get_cat_name($cat); 
    $output .= '<a href="' . $catlink . '">' . $catname . '</a>' . "\n"; 
    } 
} 

echo $output; 

這是把我的頭頂部,但我相信這是正確的。

+0

+1 ...優秀的答案雪莉!這應該是最完美的工作! – tollmanz 2011-04-27 20:45:23

+0

感謝shelly的答案,但我不是php的專家,所以我的完整代碼應該像下面那樣? – Fask 2011-04-28 15:43:47

+0

好吧 - 原諒我 - 我是這些論壇上的n00b :)我找不到「評論」鏈接來評論你的問題!但是,是的,我給你的代碼應該替換「elseif(is_single()&&!is_attachment()){」部分中的內容 - 我相信這就是你在下面的內容。再說一遍,這段代碼不在我頭頂,所以你可能需要玩一下 - 但我認爲這是正確的。希望它有助於:) – Shelly 2011-04-29 13:13:19

0

謝謝shelly答案,但我不是php的專家,所以我的完整代碼應該看起來像這樣?

function the_breadcrumb() { 
global $post; 
if (!is_home()) { 
    echo '<a href="'.get_option('home').'">'.home.'</a>'; 
    if (is_category()) { 
     echo "/"; 
     echo single_cat_title(); 
    } elseif(is_single() && !is_attachment()) { 
     $cat = get_the_category(); $cat = $cat[0]->term_id; 
    // next will return an array of all category ancestors, with toplevel cat being [0] 
    $ancestors = array_reverse(get_ancestors($cat, 'category'); 
    if($ancestors) { 
    // set up output 
    $output = ''; 
    foreach($ancestor as $cat) { 
    // skips cats 3 and 4 
    if($cat == '3' || $cat == '4') continue; 
    $catlink = get_category_link($cat); 
    $catname = get_cat_name($cat); 
    $output .= '<a href="' . $catlink . '">' . $catname . '</a>' . "\n"; 
    } 
    } 
    echo $output; 
     echo "/"; 
     echo get_category_parents($cat, TRUE, ' ' . $delimiter . ' '); 
     echo "/"; 
     echo the_title_shorten(45,'...'); 
    }  
    elseif (is_search()) { 
     echo "/" . cerca; 
    }  
    elseif (is_page() && $post->post_parent) { 
     echo '/<a href="'.get_permalink($post->post_parent).'">'; 
     echo get_the_title($post->post_parent); 
     echo "</a>/"; 
     echo the_title_shorten(45,'...');  
    } 
    elseif (is_page() OR is_attachment()) { 
     echo "/"; 
     echo the_title_shorten(45,'...'); 
    } 
    elseif (is_author()) { 
     echo wp_title('/Profilo'); 
     echo ""; 
    } 
    elseif (is_404()) { 
     echo "/"; 
     echo errore_404; 
    }  
    elseif (is_archive()) { 
     echo wp_title('/');  
    } 
} 
    } 
0

感謝在座的各位, 我最後得到的代碼工作:

$ancestors = array_reverse(get_ancestors(get_cat_ID(single_cat_title("", false)), 'category')); 
$cat_parent_and_cat = ''; 
if($ancestors) { 
    foreach($ancestors as $cat) {    
     //if($cat == '3' || $cat == '4') continue; // skips cats 3 and 4 
     $catlink = get_category_link($cat); 
     $catname = get_cat_name($cat); 
     $cat_parent_and_cat .= '<a href="' . $catlink . '">' . $catname . '</a>' . ' &rsaquo; ' ; 
    } 
} 
echo $cat_parent_and_cat; 

希望這將幫助別人