从get_category_parents(wordpress)排除类别
问题描述:
我正在寻找一种方法来从我的面包屑主题的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('/');
}
}
}
答
$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;
这是把我的头顶部,但我相信这是正确的。
答
谢谢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('/');
}
}
}
答
感谢在座的各位, 我最后得到的代码工作:
$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>' . ' › ' ;
}
}
echo $cat_parent_and_cat;
希望这将帮助别人
+1 ...优秀的答案雪莉!这应该是最完美的工作! – tollmanz 2011-04-27 20:45:23
感谢shelly的答案,但我不是php的专家,所以我的完整代码应该像下面那样? – Fask 2011-04-28 15:43:47
好吧 - 原谅我 - 我是这些论坛上的n00b :)我找不到“评论”链接来评论你的问题!但是,是的,我给你的代码应该替换“elseif(is_single()&&!is_attachment()){”部分中的内容 - 我相信这就是你在下面的内容。再说一遍,这段代码不在我头顶,所以你可能需要玩一下 - 但我认为这是正确的。希望它有助于:) – Shelly 2011-04-29 13:13:19