WordPress - Breadcrumb导航,但使用自定义的帖子类型


WordPress - Breadcrumb nav, but using custom post types

所以,我试图在我的导航栏下面使用面包屑,但是没有运气。

我遵循这个教程,但是当我开始浏览我的自定义帖子类型时,它出错了。

例如,我有产品 -在我的导航中分为内部外部产品。当我导航到其中一个-我得到以下内容:

Home»外部产品

这是伟大的!

现在-内部"外部产品"是子类别,子类别内部是产品。

如果我转到:

主页-外部产品-类别1

我希望看到:

Home»外部产品»类别1:

,而是参见:

Home»Products

是否有一个插件-或任何其他方法来解决这个问题,甚至可能自己设置路径?

我知道这可能有点晚了,但以防万一您还没有解决这个问题,我已经编写了一个您可能感兴趣的breadcrumb函数。

在写了几个WordPress站点和主题之后,我们经常需要一个breadcrumb功能,我们决定创建一个可重用的,支持任何自定义帖子类型和分类的功能。

只需在您的functions.php文件中删除代码,并在您想要显示面包屑的主题模板文件中调用该函数!

如果你需要任何帮助,请告诉我。

谢谢,亚当

https://github.com/mobilejazz/MJ-WP-Breadcrumb

对于自定义帖子类型,有三个主要模板

  1. <
  2. 分类/gh>

所以你可以添加面包屑而不使用任何插件。下面是带模式标记的代码,可以帮助你提高SEO排名

存档页面:

<ol itemscope itemtype="http://schema.org/BreadcrumbList">
  <li itemprop="itemListElement" itemscope itemtype="http://schema.org/ListItem">
    <meta itemprop="position" content="1"/>
    <a itemscope itemtype="http://schema.org/Thing" itemprop="item" href="https://your-domain-name.com/post-type-name">
      <span itemprop="name">Post type product(for eg:)</span>
    </a>
  </li>
</ol>

分类页面:

<div class="breadcrumbs">
                      <ol itemscope itemtype="http://schema.org/BreadcrumbList">
                        <li itemprop="itemListElement" itemscope itemtype="http://schema.org/ListItem">
                          <meta itemprop="position" content="1"/>
                          <a itemscope itemtype="http://schema.org/Thing" itemprop="item" href="https://youdomain/products">
                            <span itemprop="name">Products</span>
                          </a>
                        </li>
                        >
                        <li itemprop="itemListElement" itemscope itemtype="http://schema.org/ListItem">
                          <meta itemprop="position" content="2"/>
                          <a itemscope itemtype="http://schema.org/Thing" itemprop="item" href="<?php echo esc_attr(get_term_link($term, $cat)); ?>">
                            <span itemprop="name">
                              <?php 
                                  $categories = single_term_title("", false);
                                  echo $categories; 
                              ?> category name
                            </span>
                          </a>
                        </li>
                      </ol>
                    </div>

单页:

<div class="breadcrumbs">
              <ol itemscope itemtype="http://schema.org/BreadcrumbList">
                <li itemprop="itemListElement" itemscope itemtype="http://schema.org/ListItem">
                  <meta itemprop="position" content="1"/>
                  <a itemscope itemtype="http://schema.org/Thing" itemprop="item" href="https://yourdomain.com/products">
                    <span itemprop="name">Products</span>
                  </a>
                  <span class="breadcrumb-arrow">
                    >
                  </span>
                </li>
               
                <li itemprop="itemListElement" itemscope itemtype="http://schema.org/ListItem">
                  <meta itemprop="position" content="2"/>
                  <?php
                    $cat_name = 'your-taxonomy';
                    $categories = get_the_terms( $post->ID, $cat_name );
                    foreach($categories as $category) : 
                      if(!$category->parent): ?>
                        <span>
                          <a itemscope itemtype="http://schema.org/Thing" itemprop="item" href="<?php echo esc_attr(get_term_link($category, $cat_name)); ?>">
                            <?php echo $category->name; ?>
                          </a>
                        </span>
                          <span class="breadcrumb-arrow">
                            >
                          </span>
                    <?php endif; ?>
                    </li>
                    <li itemprop="itemListElement" itemscope itemtype="http://schema.org/ListItem">
                       <meta itemprop="position" content="3"/>
                      <?php
                        endforeach;
                      
                        foreach($categories as $category) : 
                        if($category->parent): ?>
                              <?php echo $category->name; ?>
                        <?php endif; 
                          endforeach;
                        ?>
                  </li>
                  <li itemprop="itemListElement" itemscope itemtype="http://schema.org/ListItem">
                    <meta itemprop="position" content="4"/>
                    <span class="subcat trim-hc-b-title" title="<?php echo the_title(); ?>">
                      <?php the_title();?>
                    </span>
                  </li>
              </ol>
            </div>