我正在使用一个名为WP Category Permalink的wordpress插件,如果有一些与帖子关联的类别,您可以选择一个特定类别作为帖子的永久链接 .
该插件运行良好,并显示我想要的父类别我've selected, however i'米也使用Yoast SEO插件中提供的breadcrumb工具,我希望面包屑遵循由WP Category Permalink插件确定的相同永久链接结构 . 面包屑只使用父类别的默认显示,并按字母顺序显示第一个 .
在查看Category Permalink插件的支持部分后,似乎有人有同样的想法,并提供以下代码,他使用了另一个breadcrumb插件:
add_filter("get_the_categories","chr_primary_category");
function chr_primary_category($cats){
global $post;
$chr_catmeta = get_post_meta($post->ID,"_category_permalink",true);
if($chr_catmeta == "") {
return $cats; // save our energy if there's no primary category set
}
$specialcat = "";
foreach($cats as $key=>$cat){
if($cat->term_id == $chr_catmeta){
$specialcat = $cat;
unset($cats[$key]);
}
}
if($specialcat != ""){
$cats = array_merge(array($specialcat),$cats); // merge it all back together, with our special category first
}
return $cats;
}
但我不知道如何利用它,所以希望有人有一些想法或尝试类似的东西 . 我甚至无法在没有抛出错误的情况下回显chr_primary_category函数数组 .
谢谢