简体   繁体   English

如何创建主页/博客名称/类别/页面标题的通用面包屑?

[英]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>&nbsp;/&nbsp;';
  echo '<a href="'.get_post_type_archive_link('post-id').'" rel="nofollow">'.get_the_archive_title().'</a>';

  if (is_category() || is_single()) {
      echo "&nbsp;/&nbsp;";
      the_category('&nbsp;/&nbsp;');
      
  } elseif (is_page()) {
      echo "&nbsp;/&nbsp;";
      echo the_title();

  } elseif (is_search()) {
      echo "&nbsp;/&nbsp;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.

相关问题 如何在 PHP 中的博客文章标题之前管理类别名称? - How to manage category name before blog post slug title in PHP? WordPress:如何将类别页面标题更改为仅类别名称? - WordPress: how to change the category page title to just the category name? 如何在博客主页上显示不包括一个类别的帖子? - How do I display posts excluding one category on blog home page? 如何在Wordpress静态首页的“主页”中删除面包屑 - How to remove breadcrumbs if “home” in wordpress static front page WordPress-如何在商店类别页面上修复WooCommerce面包屑? - WordPress - How to fix WooCommerce breadcrumbs on shop category page? 如何仅从主页上的面包屑中删除结尾的双箭头“»” - How to remove the trailing double arrows “»” from breadcrumbs on the home page only 如何在PyroCMS中将面包屑的标题和URL从博客更改为新闻 - How do I change the breadcrumbs title and url from blog to news in PyroCMS 页面标题中包含的类别和产品名称标准 - Category and product name standard included in the page title Wordpress帮助:如何将自定义博客页面用于博客类别 - Wordpress Help: How to use custom blog page for blog category 如何在Prestashop中将类别页面设置为主页 - How to Set Category Page as Home Page in Prestashop
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM