如何解决获取 CPT 页面的顶级父类别名称和 URL
我在我的functions.php 中使用以下代码来获取CPT 页面的父类别。
// Get Parent Category Link
function get_parent_category () {
$terms = get_the_terms($post->ID,'section' );
if ($terms && ! is_wp_error($terms)) :
$tslugs_arr = array();
foreach ($terms as $term) {
$tslugs_arr[] = $term->slug;
}
$terms_slug_str = join( " ",$tslugs_arr);
endif;
$categories = get_the_terms( $post->ID,'section' );
{
foreach ( $terms as $term ) {
$term_link = get_term_link( $term );
if ( is_wp_error( $term_link ) ) {
continue;
}
return esc_url( $term_link ) ;
// echo $term->name ;
}
}
}
// Get Parent Category Name
function get_parent_category_name () {
$terms = get_the_terms($post->ID,'section' );
{
foreach ( $terms as $term ) {
$term_link = get_term_link( $term );
if ( is_wp_error( $term_link ) ) {
continue;
}
//return esc_url( $term_link ) ;
return $term->name ;
}
}
}
我还需要获取同一页面的顶级类别名称和 url,我无法弄清楚如何获取顶级类别并需要它以便我可以在自定义面包屑路径中显示。
然后我使用以下代码在我的主题模板中显示父类别。
$crumbs[3]['label'] = get_parent_category_name ();
$crumbs[3]['url'] = get_parent_category () ;
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。