我无法理解仅粘贴链接的答案如何获得这么多的赞成票。常规的 WordPress 面包屑方法没有经过优化,大多数方法都不适合自定义主题。我决定构建一个基于 URL 的面包屑,从我的角度来看,它更加高效和适应性强。我想要一些通用的、对 SEO 友好的、没有任何默认样式的东西。它还需要正确处理帖子和页面标题。
|
Version |
| Requires at least WordPress: |
3.0.0 |
| Requires PHP: |
8.0 |
| Tested up to WordPress: |
5.8.2 |
最新版本在我的GitHub 上作为非官方 WordPress 插件提供。
<?php
if ( ! function_exists( 'get_the_crumbs' ) ) {
/**
* Retrieve the crumbs.
*
* @since 1.0.0
*
* @return Array Crumbs array.
*/
function get_the_crumbs() {
$flour = $_SERVER['REQUEST_URI'];
if ( str_contains( $flour, '?' ) ) {
$flour = substr( $flour, 0, strpos( $flour, '?' ) );
};
if ( str_ends_with( $flour, '/' ) ) {
$flour = explode( '/', substr( $flour, 1, -1 ) );
} else {
$flour = explode( '/', substr( $flour, 1 ) );
};
$crumbs = array();
foreach ( $flour as $crumb ) {
$slug = esc_html( $crumb );
$url = esc_url( $_SERVER['REQUEST_SCHEME'] . '://' . $_SERVER['HTTP_HOST'] . '/' . substr( implode( '/', $flour ), 0, strpos( implode( '/', $flour ), $crumb ) ) . $crumb. '/' );
array_push( $crumbs,
array(
'slug' => $slug,
'url' => $url,
)
);
};
$banned_slugs = array();
//round up all post types
$post_types = get_post_types(
array(
'public' => true,
),
'objects'
);
foreach ( $post_types as $post_type ) {
array_push( $banned_slugs, $post_type->name );
if ( isset( $post_type->rewrite['slug'] ) ) array_push( $banned_slugs, $post_type->rewrite['slug'] );
};
//round up all taxonomies
$taxonomies = get_taxonomies(
array(
'public' => true,
),
'objects'
);
foreach ( $taxonomies as $taxonomy ) {
array_push( $banned_slugs, $taxonomy->name );
if ( isset( $taxonomy->rewrite['slug'] ) ) array_push( $banned_slugs, $taxonomy->rewrite['slug'] );
};
$banned_crumbs = array();
foreach ( $banned_slugs as $banned_slug ) {
$slug = esc_html( $banned_slug );
$url = esc_url( $_SERVER['REQUEST_SCHEME'] . '://' . $_SERVER['HTTP_HOST'] . '/' . substr( implode( '/', $flour ), 0, strpos( implode( '/', $flour ), $banned_slug ) ) . $banned_slug. '/' );
array_push( $banned_crumbs,
array(
'slug' => $slug,
'url' => $url,
)
);
};
$crumbs = array_filter( $crumbs, function( $crumb ) use ( $banned_slugs ) {
if ( ! in_array( $crumb['slug'], $banned_slugs ) && ! in_array( $crumb['url'], $banned_slugs ) ) {
return ! in_array( $crumb['slug'], $banned_slugs );
};
} );
return $crumbs;
};
};
if ( ! function_exists( 'the_bread' ) ) {
/**
* Display the bread, a formatted crumbs list.
*
* @since 1.0.0
*
* @param Array $ingredients The bread arguments.
* @param Array $ingredients['root'] Root crumb. Default to null.
* @param String $ingredients['root']['slug'] Root crumb slug.
* @param String $ingredients['root']['url'] Root crumb url.
* @param String $ingredients['separator'] The crumb's separator. The separator is not escaped.
* @param Integer $ingredients['offset'] Crumbs offset. Accept positive/negative Integer. Default to "0". Refer to array_slice, https://www.php.net/manual/en/function.array-slice.php.
* @param Integer $ingredients['length'] Crumbs length. Accept positive/negative Integer. Default to "null". Refer to array_slice, https://www.php.net/manual/en/function.array-slice.php.
*
* @return Array The formatted crumbs list.
*/
function the_bread( $ingredients = array() ) {
$root = ( empty( $ingredients['root'] ) ? null : $ingredients['root'] );
$offset = ( empty( $ingredients['offset'] ) ? 0 : $ingredients['offset'] );
$length = ( empty( $ingredients['length'] ) ? null : $ingredients['length'] );
$crumbs = get_the_crumbs();
if ( ! empty( $root ) ) {
array_unshift( $crumbs, $ingredients['root'] );
};
$crumbs = array_slice( $crumbs, $offset, $length );
if ( ! empty( $crumbs ) ) {
echo '<ol class="? bread" itemscope itemtype="https://schema.org/BreadcrumbList">';
$i = 0;
foreach ( $crumbs as $crumb ) {
$i++;
if ( url_to_postid( $crumb['url'] ) ) {
$title = get_the_title( url_to_postid( $crumb['url'] ) );
} elseif ( get_page_by_path( $crumb['slug'] ) ) {
$title = get_the_title( get_page_by_path( $crumb['slug'] ) );
} else {
$title = ucfirst( str_replace( '-', ' ', $crumb['slug'] ) );
};
echo '<li class="crumb" itemprop="itemListElement" itemscope itemtype="https://schema.org/ListItem">
<a itemprop="item" href="' . $crumb['url'] . '">
<span itemprop="name">' . $title . '</span>
</a>
<meta itemprop="position" content="' . $i . '">
</li>';
if ( $i !== sizeof( $crumbs ) && ! empty( $ingredients['separator'] ) ) {
echo $ingredients['separator'];
};
};
echo '</ol>';
};
};
};
显示面包,一个格式化的面包屑列表。
<?php
the_bread( $ingredients = array() );
参数
| Parameter |
Description |
$ingredients |
(Optional) Array of arguments for displaying the bread. |
$ingredients['root'] |
Array Root crumb. Default to null. |
$ingredients['root']['slug'] |
(Required if $ingredients['root']). Root crumb slug. |
$ingredients['root']['url'] |
(Required if $ingredients['root']). Root crumb url. |
$ingredients['separator'] |
The crumb's separator. The separator is not escaped. |
$ingredients['offset'] |
Crumbs offset. Accept positive/negative Integer. Default to 0. Refer to array_slice. |
$ingredients['length'] |
Crumbs length. Accept positive/negative Integer. Default to null. Refer to array_slice. |
示例:带有自定义分隔符的面包
<?php
$ingredients = array(
'separator' => '→',
);
the_bread( $ingredients );
示例:显示最后 3 个碎屑
<?php
$ingredients = array(
'offset' => -3,
'length' => 3,
);
the_bread( $ingredients );
示例:带有根屑的面包
<?php
$ingredients = array(
'root' => array(
'slug' => 'home',
'url' => get_home_url(),
),
);
the_bread( $ingredients );
HTML5 结构输出
<ol class="? bread" itemscope="" itemtype="https://schema.org/BreadcrumbList">
<li class="crumb" itemprop="itemListElement" itemscope="" itemtype="https://schema.org/ListItem">
<a itemprop="item" href="http://example.com/where/">
<span itemprop="name">Where</span>
</a>
<meta itemprop="position" content="1">
</li>
>
<li class="crumb" itemprop="itemListElement" itemscope="" itemtype="https://schema.org/ListItem">
<a itemprop="item" href="http://example.com/where/is/">
<span itemprop="name">Is</span>
</a>
<meta itemprop="position" content="2">
</li>
>
<li class="crumb" itemprop="itemListElement" itemscope="" itemtype="https://schema.org/ListItem">
<a itemprop="item" href="http://example.com/where/is/my/">
<span itemprop="name">My</span>
</a>
<meta itemprop="position" content="3">
</li>
>
<li class="crumb" itemprop="itemListElement" itemscope="" itemtype="https://schema.org/ListItem">
<a itemprop="item" href="http://example.com/where/is/my/bread/">
<span itemprop="name">Bread</span>
</a>
<meta itemprop="position" content="4">
</li>
</ol>
最小的 css 样板(可选)
.?,
.bread {
list-style-type: none;
margin:0;
padding:0;
}
.? li,
.bread li {
display:inline-block;
}
.? li.crumb:last-child a,
.bread li.crumb:last-child a {
text-decoration: none;
pointer-events: none;
color: inherit;
}
取回碎屑
即使我们建议您使用the_bread() 函数来显示和构建自己的面包屑,您也可以使用get_the_crumbs() 来检索面包屑对象。
示例:输出 crumbs 对象
<?php
var_dump( get_the_crumbs() );