【问题标题】:Creating breadcrumbs without plugin WordPress在没有插件 WordPress 的情况下创建面包屑
【发布时间】:2018-06-17 05:34:04
【问题描述】:

当我们单击主菜单任何页面时,我如何创建面包屑home->page->post 名称,打开该时间面包屑创建home ->page 名称的帖子列表,但现在当我们单击任何帖子时,该时间面包屑创建@987654323 @ category name->post name 是当我们点击显示不同的面包屑布局上的帖子类别名称时,我们希望其继续页面链接,而不是类别链接。因此,当我们打开任何帖子时,我们需要创建像 home->page name->post 这样的面包屑名称,所以当我们点击页面名称时,打开帖子列表页面,而不是类别页面。

【问题讨论】:

  • 您的意思是仅在标题下显示一行带有面包屑的行还是仅具有 url 的链接结构?

标签: wordpress breadcrumbs


【解决方案1】:

WordPress 不提供内置的面包屑功能。因此,您必须要么使用插件,要么自己编写代码(或从下面的参考资料中复制)。

事实上,插件或自定义代码,如果提供类似的功能,并没有太大的区别。因此,请使用对您更方便的一种。

如果您想添加自定义代码,我可以在搜索中查找以下资源:

https://www.techpulsetoday.com/wordpress-breadcrumbs-without-plugin/

https://www.thewebtaylor.com/articles/wordpress-creating-breadcrumbs-without-a-plugin

https://www.codexworld.com/wordpress-how-to-display-breadcrumb-without-plugin/

https://gist.github.com/tinotriste/5387124

您可以查看它们并根据需要修改它们!

希望对你有帮助!

【讨论】:

  • 第一个工作顺利。奖励 - 它既漂亮又紧凑,不会做那种恼人的间隔代码。
  • 应避免仅链接的答案。
【解决方案2】:

我无法理解仅粘贴链接的答案如何获得这么多的赞成票。常规的 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() );

【讨论】:

  • 这真的很简洁,重量很轻。到目前为止,我看到的唯一问题是没有任何静态永久链接部分的处理,例如/category/,它没有任何内容,也没有页面ID。不过,我可能可以对其进行自定义,使其不打印出来。
  • 我没有注意到 Github 链接,也没有注意到有更高版本。我去看看。是的,我猜我可以创建那个页面。但是,我通过一系列“禁止”链接解决了这个问题,以便在打印结果之前进行检查。
  • @Athoxx Version 1.0.3 自动过滤掉帖子类型和分类法根屑。
  • 太棒了,当我回到 WP 项目时我会检查一下 :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-11-18
  • 1970-01-01
  • 2016-07-03
  • 1970-01-01
  • 2021-06-13
  • 1970-01-01
相关资源
最近更新 更多