admin管理员组文章数量:1130349
How do I adapt the following snippet to apply to a specific custom taxonomy? It current applies to post categories
I use the function below with this: <?php display_cat_level(X,true); ?> in my theme files to display each hierarchical category level
function get_level($category, $level = 0)
{
if ($category->category_parent == 0) {
return $level;
} else {
$level++;
$category = get_category($category->category_parent);
return get_level($category, $level);
}
}
function display_cat_level( $level = 0 , $link=false){
$cats = get_the_category( );
if( $cats ){
foreach($cats as $cat){
$current_cat_level = get_level($cat);
if( $current_cat_level == $level ){
if($link==true) {
echo '<a href="'.get_category_link($cat->cat_ID).'">'.$cat->name."</a>";
} else {
echo $cat->name."";
}
}
}
}
}
Answer: These updates from Jacob allows the original and updated functions to work together...
<?php
function get_level_subject($category, $level = 0)
{
if ($category->parent == 0) {
return $level;
} else {
$level++;
$category = get_term( $category->parent );
return get_level_subject($category, $level);
}
}
function display_cat_level_subject( $level = 0 , $link=false){
$cats = get_the_terms( null, 'subject' );
if( $cats ){
foreach($cats as $cat){
$current_cat_level = get_level_subject($cat);
if( $current_cat_level == $level ){
if($link==true) {
echo '<a href="'.get_term_link( $cat->term_id ).'">'.$cat->name."</a>";
} else {
echo $cat->name."";
}
}
}
}
}
?>
How do I adapt the following snippet to apply to a specific custom taxonomy? It current applies to post categories
I use the function below with this: <?php display_cat_level(X,true); ?> in my theme files to display each hierarchical category level
function get_level($category, $level = 0)
{
if ($category->category_parent == 0) {
return $level;
} else {
$level++;
$category = get_category($category->category_parent);
return get_level($category, $level);
}
}
function display_cat_level( $level = 0 , $link=false){
$cats = get_the_category( );
if( $cats ){
foreach($cats as $cat){
$current_cat_level = get_level($cat);
if( $current_cat_level == $level ){
if($link==true) {
echo '<a href="'.get_category_link($cat->cat_ID).'">'.$cat->name."</a>";
} else {
echo $cat->name."";
}
}
}
}
}
Answer: These updates from Jacob allows the original and updated functions to work together...
<?php
function get_level_subject($category, $level = 0)
{
if ($category->parent == 0) {
return $level;
} else {
$level++;
$category = get_term( $category->parent );
return get_level_subject($category, $level);
}
}
function display_cat_level_subject( $level = 0 , $link=false){
$cats = get_the_terms( null, 'subject' );
if( $cats ){
foreach($cats as $cat){
$current_cat_level = get_level_subject($cat);
if( $current_cat_level == $level ){
if($link==true) {
echo '<a href="'.get_term_link( $cat->term_id ).'">'.$cat->name."</a>";
} else {
echo $cat->name."";
}
}
}
}
}
?>
Share
Improve this question
edited Nov 23, 2018 at 12:31
Pete
asked Nov 23, 2018 at 11:41
PetePete
1,0582 gold badges14 silver badges40 bronze badges
1 Answer
Reset to default 1Change:
get_the_category()toget_the_terms( null, 'taxonomy_name' ).get_category_link( $cat->cat_ID )toget_term_link( $cat->term_id ).get_category( $category->category_parent )toget_term( $category->parent ).
If you want to use the same function for multiple taxonomies, you can accept the taxonomy name as an argument and pass it to the first item above:
function display_cat_level( $level = 0 , $link = false, $taxonomy = 'category' ){
$cats = get_the_terms( null, $taxonomy );
// etc.
}
Also, even when working with categories, don't use cat_ID and category_parent. Those were deprecated 11 years ago in favour of term_id and parent.
How do I adapt the following snippet to apply to a specific custom taxonomy? It current applies to post categories
I use the function below with this: <?php display_cat_level(X,true); ?> in my theme files to display each hierarchical category level
function get_level($category, $level = 0)
{
if ($category->category_parent == 0) {
return $level;
} else {
$level++;
$category = get_category($category->category_parent);
return get_level($category, $level);
}
}
function display_cat_level( $level = 0 , $link=false){
$cats = get_the_category( );
if( $cats ){
foreach($cats as $cat){
$current_cat_level = get_level($cat);
if( $current_cat_level == $level ){
if($link==true) {
echo '<a href="'.get_category_link($cat->cat_ID).'">'.$cat->name."</a>";
} else {
echo $cat->name."";
}
}
}
}
}
Answer: These updates from Jacob allows the original and updated functions to work together...
<?php
function get_level_subject($category, $level = 0)
{
if ($category->parent == 0) {
return $level;
} else {
$level++;
$category = get_term( $category->parent );
return get_level_subject($category, $level);
}
}
function display_cat_level_subject( $level = 0 , $link=false){
$cats = get_the_terms( null, 'subject' );
if( $cats ){
foreach($cats as $cat){
$current_cat_level = get_level_subject($cat);
if( $current_cat_level == $level ){
if($link==true) {
echo '<a href="'.get_term_link( $cat->term_id ).'">'.$cat->name."</a>";
} else {
echo $cat->name."";
}
}
}
}
}
?>
How do I adapt the following snippet to apply to a specific custom taxonomy? It current applies to post categories
I use the function below with this: <?php display_cat_level(X,true); ?> in my theme files to display each hierarchical category level
function get_level($category, $level = 0)
{
if ($category->category_parent == 0) {
return $level;
} else {
$level++;
$category = get_category($category->category_parent);
return get_level($category, $level);
}
}
function display_cat_level( $level = 0 , $link=false){
$cats = get_the_category( );
if( $cats ){
foreach($cats as $cat){
$current_cat_level = get_level($cat);
if( $current_cat_level == $level ){
if($link==true) {
echo '<a href="'.get_category_link($cat->cat_ID).'">'.$cat->name."</a>";
} else {
echo $cat->name."";
}
}
}
}
}
Answer: These updates from Jacob allows the original and updated functions to work together...
<?php
function get_level_subject($category, $level = 0)
{
if ($category->parent == 0) {
return $level;
} else {
$level++;
$category = get_term( $category->parent );
return get_level_subject($category, $level);
}
}
function display_cat_level_subject( $level = 0 , $link=false){
$cats = get_the_terms( null, 'subject' );
if( $cats ){
foreach($cats as $cat){
$current_cat_level = get_level_subject($cat);
if( $current_cat_level == $level ){
if($link==true) {
echo '<a href="'.get_term_link( $cat->term_id ).'">'.$cat->name."</a>";
} else {
echo $cat->name."";
}
}
}
}
}
?>
Share
Improve this question
edited Nov 23, 2018 at 12:31
Pete
asked Nov 23, 2018 at 11:41
PetePete
1,0582 gold badges14 silver badges40 bronze badges
1 Answer
Reset to default 1Change:
get_the_category()toget_the_terms( null, 'taxonomy_name' ).get_category_link( $cat->cat_ID )toget_term_link( $cat->term_id ).get_category( $category->category_parent )toget_term( $category->parent ).
If you want to use the same function for multiple taxonomies, you can accept the taxonomy name as an argument and pass it to the first item above:
function display_cat_level( $level = 0 , $link = false, $taxonomy = 'category' ){
$cats = get_the_terms( null, $taxonomy );
// etc.
}
Also, even when working with categories, don't use cat_ID and category_parent. Those were deprecated 11 years ago in favour of term_id and parent.
本文标签: Display a specific hierarchical level of a specific custom taxonomy
版权声明:本文标题:Display a specific hierarchical level of a specific custom taxonomy 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://it.en369.cn/questions/1749156748a2324724.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。


发表评论