admin管理员组文章数量:1122846
I have a breadcrumb displayed on all content types (pages, posts, homepage etc.) - beside of category. What is the way to add category to content types, where breadcrumb is showing?
This is the breadcrumb code from my functions.php:
function asap_breadcrumbs_pages($post,$schema)
{
if ( ! get_theme_mod('asap_hide_breadcrumb_page') && ! get_post_meta( get_the_ID(), 'hide_breadcrumbs', true ) )
{
$url_pillar_page = get_post_meta(get_the_ID() , 'single_bc_url_pillar_page', true);
$text_pillar_page = get_post_meta(get_the_ID() , 'single_bc_text_pillar_page', true);
$post_title = get_post_meta(get_the_ID() , 'single_bc_text', true) ? : get_the_title();
$label = get_theme_mod('asap_breadcrumb_text') ? : get_bloginfo('name');
if ($schema)
{
$format = '<li itemprop="itemListElement" itemscope itemtype=";><a href="%s" title="%s" itemprop="item"><span itemprop="name">%s</span></a><meta itemprop="position" content="%s"></li>';
}
else
{
$format = '<li><a href="%s" title="%s"><span>%s</span></a></li>';
}
if ($url_pillar_page && $text_pillar_page)
{
$count = 3;
}
else
{
$anc = array_map('get_post', array_reverse((array)get_post_ancestors($post)));
$count = count($anc);
$count = $count + 2;
$links = array_map('get_permalink', $anc);
}
if ($schema)
{
printf('<div role="navigation" aria-label="Breadcrumbs" class="breadcrumb-trail breadcrumbs" itemprop="breadcrumb">');
printf('<ul class="breadcrumb" itemscope itemtype=";>');
printf('<meta name="numberOfItems" content="' . $count . '">');
printf('<meta name="itemListOrder" content="Ascending">');
printf('<li itemprop="itemListElement" itemscope itemtype=";><a href="%s" itemprop="item"><span itemprop="name">' . $label . '</span></a><meta itemprop="position" content="1"></li>', esc_url(home_url()));
}
else
{
printf('<div class="breadcrumb-trail breadcrumbs">');
printf('<ul class="breadcrumb">');
printf('<li><a href="%s"><span>' . $label . '</span></a></li>', esc_url(home_url()));
}
$meta = 2;
if ($url_pillar_page && $text_pillar_page)
{
printf($format, $url_pillar_page, $text_pillar_page, $text_pillar_page, $meta);
$meta = $meta + 1;
}
else
{
foreach ($anc as $i => $apost)
{
$title = get_post_meta( $apost->ID , 'single_bc_text', true);
if ( ! $title ) {
$title = apply_filters('the_title', $apost->post_title);
}
printf($format, $links[$i], esc_attr($title) , esc_html($title) , $meta);
$meta = $meta + 1;
}
}
if ($schema)
{
echo '<li itemprop="itemListElement" itemscope itemtype=";><span itemprop="name">' . $post_title . '</span><meta itemprop="position" content="' . $meta . '"></li>';
}
else
{
echo '<li><span>' . $post_title . '</span></li>';
}
printf('</ul>');
printf('</div>');
}
}
I have a breadcrumb displayed on all content types (pages, posts, homepage etc.) - beside of category. What is the way to add category to content types, where breadcrumb is showing?
This is the breadcrumb code from my functions.php:
function asap_breadcrumbs_pages($post,$schema)
{
if ( ! get_theme_mod('asap_hide_breadcrumb_page') && ! get_post_meta( get_the_ID(), 'hide_breadcrumbs', true ) )
{
$url_pillar_page = get_post_meta(get_the_ID() , 'single_bc_url_pillar_page', true);
$text_pillar_page = get_post_meta(get_the_ID() , 'single_bc_text_pillar_page', true);
$post_title = get_post_meta(get_the_ID() , 'single_bc_text', true) ? : get_the_title();
$label = get_theme_mod('asap_breadcrumb_text') ? : get_bloginfo('name');
if ($schema)
{
$format = '<li itemprop="itemListElement" itemscope itemtype="http://schema.org/ListItem"><a href="%s" title="%s" itemprop="item"><span itemprop="name">%s</span></a><meta itemprop="position" content="%s"></li>';
}
else
{
$format = '<li><a href="%s" title="%s"><span>%s</span></a></li>';
}
if ($url_pillar_page && $text_pillar_page)
{
$count = 3;
}
else
{
$anc = array_map('get_post', array_reverse((array)get_post_ancestors($post)));
$count = count($anc);
$count = $count + 2;
$links = array_map('get_permalink', $anc);
}
if ($schema)
{
printf('<div role="navigation" aria-label="Breadcrumbs" class="breadcrumb-trail breadcrumbs" itemprop="breadcrumb">');
printf('<ul class="breadcrumb" itemscope itemtype="http://schema.org/BreadcrumbList">');
printf('<meta name="numberOfItems" content="' . $count . '">');
printf('<meta name="itemListOrder" content="Ascending">');
printf('<li itemprop="itemListElement" itemscope itemtype="http://schema.org/ListItem"><a href="%s" itemprop="item"><span itemprop="name">' . $label . '</span></a><meta itemprop="position" content="1"></li>', esc_url(home_url()));
}
else
{
printf('<div class="breadcrumb-trail breadcrumbs">');
printf('<ul class="breadcrumb">');
printf('<li><a href="%s"><span>' . $label . '</span></a></li>', esc_url(home_url()));
}
$meta = 2;
if ($url_pillar_page && $text_pillar_page)
{
printf($format, $url_pillar_page, $text_pillar_page, $text_pillar_page, $meta);
$meta = $meta + 1;
}
else
{
foreach ($anc as $i => $apost)
{
$title = get_post_meta( $apost->ID , 'single_bc_text', true);
if ( ! $title ) {
$title = apply_filters('the_title', $apost->post_title);
}
printf($format, $links[$i], esc_attr($title) , esc_html($title) , $meta);
$meta = $meta + 1;
}
}
if ($schema)
{
echo '<li itemprop="itemListElement" itemscope itemtype="http://schema.org/ListItem"><span itemprop="name">' . $post_title . '</span><meta itemprop="position" content="' . $meta . '"></li>';
}
else
{
echo '<li><span>' . $post_title . '</span></li>';
}
printf('</ul>');
printf('</div>');
}
}
Share
Improve this question
asked May 22, 2024 at 19:01
EvgeniyEvgeniy
1058 bronze badges
1 Answer
Reset to default 0If you're dealing with the default "Category" taxonomy and the post
post type, then you can get the post's attached category terms with get_the_category().
$categories = has_category( $post ) ? get_the_category( $post ) : array();
The has_category()
check is there just to fend off other post type posts.
get_the_category()
gives you an array of categories as WP_Term
s. They are probably not in any particular order and there might terms from different hierarchy levels in the list. So for example use the first term or conjure up some logic to order the terms in the desired order, before rendering them all.
To get the category link you can use get_category_link(). The category name can be read from the WP_Term
object and used as the anchor text.
$category_link = $categories
? sprintf(
'<a href="%s">%s</a>',
esc_url( get_category_link( $categories[0] ) ),
esc_html( $categories[0]->name )
)
: '';
If you have other taxonomies and post types, then use has_term(), get_the_terms(), and get_term_link().
本文标签: postsHow to display breadcrumb on category
版权声明:本文标题:posts - How to display breadcrumb on category? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736306350a1932924.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论