[英]How to create a universal breadcrumbs that is Home / Blog Name / Category / Page Title?
I am trying to create breadcrumbs for my site that is formatted Home / Blog / Category / Page or Post Name我正在尝试为格式为主页/博客/类别/页面或帖子名称的网站创建面包屑
I do not wish to use a plugin and I have tried to use the following code but its not working out well.我不想使用插件,我尝试使用以下代码,但效果不佳。
function get_blog_breadcrumb() {
echo '<a href="'.home_url().'" rel="nofollow">Home</a> / ';
echo '<a href="'.get_post_type_archive_link('post-id').'" rel="nofollow">'.get_the_archive_title().'</a>';
if (is_category() || is_single()) {
echo " / ";
the_category(' / ');
} elseif (is_page()) {
echo " / ";
echo the_title();
} elseif (is_search()) {
echo " / Search Results for... ";
echo '"<em>';
echo the_search_query();
echo '</em>"';
}
}
I like to build an array of the path of the breadcrumb first, using this format: ['name' => 'url']
.我喜欢首先使用以下格式构建面包屑路径的数组: ['name' => 'url']
。 so first:所以首先:
$arr_breadcrumbs['Home'] = get_site_url();
Then I use is_home()
, is_single()
, is_category()
to push for example ['Blog'=> get_option('page_for_posts', true)]
and so on.然后我使用is_home()
、 is_single()
、 is_category()
来推送例如['Blog'=> get_option('page_for_posts', true)]
等等。 Finish it by pushing last the title of page/post with value of null.通过最后推送值为 null 的页面/帖子的标题来完成它。 Then you can use this simple routine to print the breadcrumb:然后你可以使用这个简单的例程来打印面包屑:
function itg_the_breadcrumbs($arr_breadcrumbs)
{
foreach ($arr_breadcrumbs as $title => $url) {
if ($url) {
echo '<li class="breadcrumb-item"><a href="' . $url . '">' . $title . '</a></li>';
} else {
echo '<li class="breadcrumb-item active" aria-current="page">' . $title . '</li>';
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.