italystrap/breadcrumbs

ItalyStrap Breadcrumbs 类


README

License Latest Stable Version Codacy Badge Build Status

WordPress 的面包屑类 API

此包创建 HTML 或 Json 面包屑元素,以便在您的 WordPress 网站上显示

安装

使用 Composer 安装

将包添加到您的项目 composer.json 文件中。访问 getcomposer.org 获取更多信息。

composer require italystrap/breadcrumbs

或者

{
    "require": {
        "italystrap/breadcrumbs": "dev-master"
    }
}

手动安装

下载并将类文件包含到您的主题/插件中

include_once 'path/to/ItalyStrap/Breadcrumbs.php';

使用方法

基本用法

使用 \ItalyStrap\Breadcrumbs\Breadcrumbs_Factory::make( $type, $args ) 在模板中显示面包屑。

use ItalyStrap\Breadcrumbs;

echo Breadcrumbs_Factory::make( 'html', $args );

// Or

echo Breadcrumbs_Factory::make( 'json', $args );

第一个参数是要显示的面包屑类型

  • HTML
    • 返回 HTML 输出
  • Json
    • 返回 Json 输出
  • 对象
    • 返回对象输出
  • 数组
    • 返回数组输出

选项

可以传递一个可选的参数数组来修改面包屑输出。每个选项的默认值请参阅 Breadcrumbs/config/breadcrumbs.php

/**
 * Default configuration for Breadcrumbs
 */
return [

	/**
	 * This is the container of the breadcrumbs
	 * @example <nav aria-label="breadcrumb">...</nav>
	 */
	'container_tag'				=> 'nav',
	'container_attr'			=> [
		'aria-label'	=> 'breadcrumb',
	],

	/**
	 * This is the list tag of the breadcrumbs
	 * @example <ol class="breadcrumb" itemscope itemtype="https://schema.org/BreadcrumbList">...</ol>
	 */
	'list_tag'					=> 'ol',
	'list_attr'					=> [
		'class'			=> 'breadcrumb',
		'itemscope'		=> true,
		'itemtype'		=> 'https://schema.org/BreadcrumbList',
	],

	/**
	 * This is the item tag of the breadcrumbs
	 * @example <li class="breadcrumb-item [...active]" itemprop="itemListElement" itemscope itemtype="https://schema.org/ListItem">...</li>
	 */
	'item_tag'					=> 'li',
	'item_attr'					=> [
		'class'			=> "breadcrumb-item",
		'itemprop'		=> 'itemListElement',
		'itemscope'		=> true,
		'itemtype'		=> 'https://schema.org/ListItem',
	],
	/**
	 * Css class for active element
	 */
	'item_attr_class_active'	=> ' active',

	/**
	 * It could be passed an HTML icon to show instead of the firt element (home)
	 * @example <span class="glyphicon glyphicon-home" aria-hidden="true"></span>
	 */
	'home_icon'					=> false,

	/**
	 * Separator for the items
	 * @example ' /'
	 */
	'separator'					=> false,

	/**
	 * Show on front
	 * @default true
	 */
	'show_on_front'				=> true,
];

默认 HTML 输出

<nav aria-label="breadcrumb">
	<ol class="breadcrumb" itemscope="" itemtype="https://schema.org/BreadcrumbList">
		<meta name="numberOfItems" content="2">
		<meta name="itemListOrder" content="Ascending">
		<li class="breadcrumb-item" itemprop="itemListElement" itemscope="" itemtype="https://schema.org/ListItem">
			<a itemprop="item" href="http://192.168.1.10/italystrap/">
				<span itemprop="name">ItalyStrap</span></a>
				<meta itemprop="position" content="1">
		</li>
		<li class="breadcrumb-item active" itemprop="itemListElement" itemscope="" itemtype="https://schema.org/ListItem" aria-current="page">
			<a itemprop="item" href="http://192.168.1.10/italystrap/blog/">
				<span itemprop="name">Blog</span></a>
				<meta itemprop="position" content="2">
		</li>
	</ol>
</nav>

高级用法

HTML 版本的示例

您可以将此代码片段复制到文件 breadcrumbs.php 中,并在插件/主题中包含它。

/**
 * Get the Breadcrumbs
 *
 * @param  array  $args The breadcrumbs arguments.
 *                      @see class Breadcrumbs for more info.
 * @return string       Return the breadcrumbs html.
 */
function get_breadcrumbs( array $args = array() ) {

	$args['bloginfo_name'] = GET_BLOGINFO_NAME;
	$args['home_url'] = HOME_URL;
	$args['separator'] = false;

	$args['show_on_front'] = false;

	try {

		return apply_filters(
			'italystrap_get_the_breadcrumbs',
			\ItalyStrap\Breadcrumbs\Breadcrumbs_Factory::make( 'html', $args ),
			$args
		);

	} catch ( Exception $e ) {
		echo $e->getMessage();
	}
}

/**
 * Print the Breadcrumbs
 *
 * @param  array  $args The breadcrumbs arguments.
 *                      @see class Breadcrumbs for more info.
 * @return string       Return the breadcrumbs html.
 */
function breadcrumbs( array $args = array() ) {

	echo get_breadcrumbs( $args );
}

/**
 * Do breadcrumbs
 *
 * @since 2.2.0
 *
 * @param  array  $args The breadcrumbs arguments.
 */
function do_breadcrumbs( array $args = array() ) {

	breadcrumbs( $args );
}
add_action( 'do_breadcrumbs', __NAMESPACE__ . '\do_breadcrumbs' );

然后使用函数 do_action( 'do_breadcrumbs', [] ) 您可以在主题中希望显示面包屑的位置显示面包屑。

Json 版本的示例

/**
 * Get the Breadcrumbs
 *
 * @param  array  $args The breadcrumbs arguments.
 *                      @see class Breadcrumbs for more info.
 * @return string       Return the breadcrumbs html.
 */
function get_breadcrumbs( array $args = array() ) {

	$args['bloginfo_name'] = GET_BLOGINFO_NAME;
	$args['home_url'] = HOME_URL;

	$args['show_on_front'] = false;

	try {

		return apply_filters(
			'italystrap_get_the_breadcrumbs',
			\ItalyStrap\Breadcrumbs\Breadcrumbs_Factory::make( 'json', $args ),
			$args
		);

	} catch ( Exception $e ) {
		echo $e->getMessage();
	}
}

/**
 * Print the Breadcrumbs
 *
 * @param  array  $args The breadcrumbs arguments.
 *                      @see class Breadcrumbs for more info.
 * @return string       Return the breadcrumbs html.
 */
function breadcrumbs( array $args = array() ) {

	echo get_breadcrumbs( $args );
}

/**
 * Do breadcrumbs
 *
 * @since 2.2.0
 *
 * @param  array  $args The breadcrumbs arguments.
 */
function do_breadcrumbs( array $args = array() ) {

	breadcrumbs( $args );
}
add_action( 'wp_footer', __NAMESPACE__ . '\do_breadcrumbs' );

过滤器

待办事项

其他示例

待办事项

array_insert()

array_insert() 是一个允许您在特定索引处将新元素插入数组的函数。

array_insert() 示例

/**
 * Modify breadcrums list
 *
 * @param  {array} $list
 *
 * @return {array}
 */
function modify_breadcrumbs_list( array $list ) {

    // if on the events category archive page
    if( is_tax( 'event-categories' ) ) {

        // create a new element
        $element = [
            'title'	=> "Shows",
            'url'	=> site_url( '/shows' )
        ];

        // add the new element at the index of 1
        $list = array_insert( $list, $element, 1 );
    }

    return $list;
}

add_filter( 'ItalyStrap\Breadcrumbs\Container\Items', 'modify_breadcrumbs_list' );

注意事项

作者

Enea Overclokk