创建没有插件的面包屑

GAN*_*ANE 3 wordpress breadcrumbs

home->page->post当我们单击主菜单中的任何页面时,如何创建面包屑名称,打开该时间创建面包屑home ->page名称的帖子列表,但是现在当我们单击任何时间时,面包屑创建home->post类别name->post名称就是当我们单击帖子类别时面包屑布局上的名称显示不同,我们希望其转到页面链接,而不是类别链接。因此,当我们打开任何帖子时,我们都需要创建类似于该home->page name->post名称的面包屑,因此,当我们单击页面名称时,请打开帖子列表页面,而不是类别页面。

ama*_*ary 13

我无法理解仅粘贴链接的答案如何能够获得这么多的赞成票。常规的 WordPress 面包屑方法未经过优化,令人痛苦,大多数方法都不适合自定义主题。我决定构建一个基于 URL 的面包屑,在我看来,它更加高效且适应性更强。我想要一些通用的、SEO 友好的、没有任何默认样式的东西。它还需要正确处理帖子和页面标题。

\n
\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
版本
至少需要 WordPress:5.0.0
至少需要 PHP:7.0.0
已测试 WordPress:6.0.2
\n
\n
\n

最新版本可在我的GitHub上作为非官方 WordPress 插件获取。

\n
\n
<?php\n\n/**\n * Checks if a string ends with a given substring.\n * Backward compatibility for PHP < 8.0.0.\n *\n * @since   1.2.0\n * @param   String  $haystack   The string to search in.\n * @param   String  $needle     The substring to search for in the haystack.\n * @return  Boolean\n */\nif ( ! function_exists( \'backward_compatibility_str_ends_with\' ) ) {\n\n    function backward_compatibility_str_ends_with( $haystack, $needle ) {\n\n        $length = strlen( $needle );\n\n        if ( ! $length ) {\n\n            return true;\n\n        };\n\n        return substr( $haystack, -$length ) === $needle;\n\n    };\n\n};\n\n/**\n * Determine if a string contains a given substring.\n * Backward compatibility for PHP < 8.0.0.\n *\n * @since   1.2.0\n * @param   String  $haystack   The string to search in.\n * @param   String  $needle     The substring to search for in the haystack.\n * @return  Boolean\n */\nif ( ! function_exists( \'backward_compatibility_str_contains\' ) ) {\n\n    function backward_compatibility_str_contains( $haystack, $needle ) {\n\n        if ( strpos( $haystack, $needle ) !== false ) {\n\n            return true;\n\n        };\n\n    };\n\n};\n\n/**\n * Retrieve the crumbs.\n * \n * @since   1.0.0\n * @return  Array   Crumbs array.\n */\nif ( ! function_exists( \'get_the_crumbs\' ) ) {\n\n    function get_the_crumbs() {\n\n        /**\n         * $_SERVER["REQUEST_SCHEME"] seems to be UNRELIABLE.\n         * \n         * Article "Is $_SERVER[\'REQUEST_SCHEME\'] reliable?".\n         * @see https://stackoverflow.com/a/18008178/3645650\n         * \n         * $_SERVER[\'REQUEST_SCHEME\'] is a native variable of Apache web server since its version 2.4.\n         * Naturally, if a variable is not set by the server, PHP will not include it in its global array $_SERVER.\n         * \n         * An alternative to $_SERVER[\'REQUEST_SCHEME\'] is $_SERVER[\'HTTPS\'] which set to a non-empty value if the script was queried through the HTTPS protocol.\n         * \n         * Article "How to find out if you\'re using HTTPS without $_SERVER[\'HTTPS\']".\n         * @see https://stackoverflow.com/a/16076965/3645650\n         */\n\n        if ( isset( $_SERVER[\'HTTPS\'] ) && $_SERVER[\'HTTPS\'] == \'on\' ) {\n\n            $server_scheme = \'https\';\n\n        } elseif ( ! empty( $_SERVER[\'HTTP_X_FORWARDED_PROTO\'] ) && $_SERVER[\'HTTP_X_FORWARDED_PROTO\'] == \'https\' || ! empty( $_SERVER[\'HTTP_X_FORWARDED_SSL\'] ) && $_SERVER[\'HTTP_X_FORWARDED_SSL\'] == \'on\' ) {\n            \n            $server_scheme = \'https\';\n\n        } else {\n\n            $server_scheme = \'http\';\n\n        };\n        \n        /**\n         * $_SERVER["REQUEST_URI"] seems to be RELIABLE.\n         * $_SERVER[\'REQUEST_URI\'] will not be empty in WordPress, because it is filled in wp_fix_server_vars() (file wp-includes/load.php).\n         * \n         * Article "Is it safe to use $_SERVER[\'REQUEST_URI\']?".\n         * @see https://wordpress.stackexchange.com/a/110541/190376\n         */\n        $server_uri = $_SERVER[\'REQUEST_URI\'];\n\n        /**\n         * $_SERVER["HTTP_HOST"] seems to be RELIABLE.\n         * \n         * Article "How reliable is HTTP_HOST?".\n         * @see https://stackoverflow.com/a/4096246/3645650\n         */\n        $server_host = $_SERVER["HTTP_HOST"];\n\n        if ( backward_compatibility_str_contains( $server_uri, \'?\' ) ) {\n\n            $server_uri = substr( $server_uri, 0, strpos( $server_uri, \'?\' ) );\n\n        };\n\n        if ( backward_compatibility_str_ends_with( $server_uri, \'/\' ) ) {\n\n            $server_uri = explode( \'/\', substr( $server_uri, 1, -1 ) );\n\n        } else {\n\n            $server_uri = explode( \'/\', substr( $server_uri, 1 ) );\n\n        };\n\n        $crumbs = array();\n\n        foreach ( $server_uri as $crumb ) {\n\n            $slug = esc_html( urldecode( $crumb ) );\n\n            $url = esc_url( $server_scheme . \'://\' . $server_host . \'/\' . substr( implode( \'/\', $server_uri ), 0, strpos( implode( \'/\', $server_uri ), $crumb ) ) . $crumb. \'/\' );\n\n            array_push( $crumbs, \n                array(\n                    \'slug\' => $slug,\n                    \'url\' => $url,\n                )\n            );\n\n        };\n\n        /**\n         * WordPress, by default, doesn\'t generate a taxonomy index, meaning https://.../taxonomy will redirect to a 404.\n         * Any request needs to be made against a term. eg: https://.../taxonomy/term will redirect to taxonomy.php.\n         * Therefore we need to remove the taxonomy slug from the crumbs array to avoid displaying a link to a 404.\n         * \n         * We round up all taxonomies through get_taxonomies(). \n         * @see https://developer.wordpress.org/reference/functions/get_taxonomies/\n         * \n         * Through array_filter we filter-out any matching crumbs.\n         * @see https://www.php.net/manual/en/function.array-filter.php\n         */\n        $banned_slugs = array();\n\n        $taxonomies = get_taxonomies( \n            array(\n                \'public\' => true,\n            ),\n            \'objects\'\n        );\n        \n        foreach ( $taxonomies as $taxonomy ) {\n\n            array_push( $banned_slugs, $taxonomy->name );\n            \n            if ( isset( $taxonomy->rewrite[\'slug\'] ) ) {\n            \n                array_push( $banned_slugs, $taxonomy->rewrite[\'slug\'] );\n            \n            };\n\n        };\n\n        $banned_crumbs = array();\n\n        foreach ( $banned_slugs as $banned_slug ) {\n\n            $slug = esc_html( $banned_slug );\n\n            $url = esc_url( $server_scheme . \'://\' . $server_host . \'/\' . substr( implode( \'/\', $server_uri ), 0, strpos( implode( \'/\', $server_uri ), $banned_slug ) ) . $banned_slug. \'/\' );\n\n            array_push( $banned_crumbs, \n                array(\n                    \'slug\' => $slug,\n                    \'url\' => $url,\n                )\n            );\n\n        };\n\n        $crumbs = array_filter( $crumbs, function( $crumb ) use ( $banned_slugs ) {\n\n            if ( ! in_array( $crumb[\'slug\'], $banned_slugs ) && ! in_array( $crumb[\'url\'], $banned_slugs ) ) {\n\n                return ! in_array( $crumb[\'slug\'], $banned_slugs );\n\n            };\n\n        } );\n\n        return $crumbs;\n\n    };\n\n};\n\n/**\n * Display the bread, a formatted crumbs list.\n * \n * @since   1.0.0\n * @param   Array   $ingredients                    The bread arguments.\n * @param   Array   $ingredients[\'crumbs\']          The crumbs array. Default to get_the_crumbs().\n * @param   Array   $ingredients[\'root\']            Root crumb. Default to null.\n * @param   String  $ingredients[\'root\'][\'slug\']    Root crumb slug.\n * @param   String  $ingredients[\'root\'][\'url\']     Root crumb url.\n * @param   String  $ingredients[\'separator\']       The crumb\'s separator.\n * @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.\n * @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.\n * @return  Array   The formatted crumbs list.\n */\nif ( ! function_exists( \'the_bread\' ) ) {\n\n    function the_bread( $ingredients = array() ) {\n\n        if ( empty( $ingredients[\'crumbs\'] ) ) {\n        \n            $crumbs = get_the_crumbs();\n            \n        } else {\n        \n            $crumbs = $ingredients[\'crumbs\'];\n            \n        };\n\n        if ( empty( $ingredients[\'root\'] ) ) {\n        \n            $root = null;\n            \n        } else {\n        \n            $root = $ingredients[\'root\'];\n            \n        };\n        \n        if ( empty( $ingredients[\'offset\'] ) ) {\n        \n            $offset = 0;\n            \n        } else {\n        \n            $offset = $ingredients[\'offset\'];\n            \n        };\n               \n        if ( empty( $ingredients[\'length\'] ) ) {\n        \n            $length = null;\n            \n        } else {\n        \n            $length = $ingredients[\'length\'];\n            \n        };\n\n        /**\n         * Handling the root crumb case. \n         * Prepend one or more elements to the beginning of an array.\n         * @see https://www.php.net/manual/en/function.array-unshift.php\n         */\n        if ( ! empty( $root ) ) {\n\n            array_unshift( $crumbs, $ingredients[\'root\'] );\n\n        };\n        \n        /**\n         * Handling the length case.\n         * Extract a slice of the array.\n         * @see https://www.php.net/manual/en/function.array-slice.php\n         */\n        $crumbs = array_slice( $crumbs, $offset, $length );\n\n        if ( ! empty( $crumbs ) ) {\n\n            echo \'<ol class=" bread" itemscope itemtype="https://schema.org/BreadcrumbList">\';\n\n            $i = 0;\n            \n            foreach ( $crumbs as $crumb ) {\n\n                $i++;\n\n                /**\n                 * Unparsing the slug.\n                 */\n                if ( url_to_postid( $crumb[\'url\'] ) ) {\n\n                    $title = get_the_title( url_to_postid( $crumb[\'url\'] ) );\n\n                } elseif ( get_page_by_path( $crumb[\'slug\'] ) ) {\n\n                    $title = get_the_title( get_page_by_path( $crumb[\'slug\'] ) );\n\n                } else {\n  \n                    $title = ucfirst( str_replace( \'-\', \' \', $crumb[\'slug\'] ) );\n\n                };\n\n                echo \'<li class="crumb" itemprop="itemListElement" itemscope itemtype="https://schema.org/ListItem">\n                    <a itemprop="item" href="\' . $crumb[\'url\'] . \'">\n                        <span itemprop="name">\' . $title . \'</span>\n                    </a>\n                    <meta itemprop="position" content="\' . $i . \'">\n                </li>\';\n\n                if ( $i !== sizeof( $crumbs ) && ! empty( $ingredients[\'separator\'] ) ) {\n\n                    echo $ingredients[\'separator\'];\n\n                };\n    \n            };\n    \n            echo \'</ol>\';\n\n        };\n\n    };\n\n};\n
Run Code Online (Sandbox Code Playgroud)\n

显示面包,格式化的面包屑列表。

\n
<?php\n\nthe_bread( $ingredients = array() );\n
Run Code Online (Sandbox Code Playgroud)\n

参数

\n
\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n \n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
范围描述
$ingredients(可选)Array面包参数。
$ingredients[\'crumbs\']Array面包屑阵列。默认为get_the_crumbs().
$ingredients[\'root\']Array根屑。默认为null.
$ingredients[\'root\'][\'slug\'](如果是必需的$ingredients[\'root\'])。根屑蛞蝓。
$ingredients[\'root\'][\'url\'](如果是必需的$ingredients[\'root\'])。根屑 url。
$ingredients[\'separator\']面包屑的分离器。
$ingredients[\'offset\']面包屑偏移。接受正/负Integer。默认为0. 请参阅array_slice
$ingredients[\'length\']面包屑长度。接受正/负Integer。默认为null. 请参阅array_slice
\n

示例:带有自定义分隔符的面包

\n
<?php\n\n$ingredients = array(\n    \'separator\' => \'\xe2\x86\x92\',\n);\n\nthe_bread( $ingredients );\n
Run Code Online (Sandbox Code Playgroud)\n

示例:显示最后 3 个面包屑

\n
<?php\n\n$ingredients = array(\n    \'offset\' => -3,\n    \'length\' => 3,\n);\n\nthe_bread( $ingredients );\n
Run Code Online (Sandbox Code Playgroud)\n

示例:带有根屑的面包

\n
<?php\n\n$ingredients = array(\n    \'root\' => array(\n        \'slug\' => \'home\',\n        \'url\' => get_home_url(),\n    ),\n);\n\nthe_bread( $ingredients );\n
Run Code Online (Sandbox Code Playgroud)\n

示例:拦截crumbs数组

\n
<?php\n\n//Intercept the crumbs array...\n$crumbs = get_the_crumbs();\n\n//... Do something with it:\n//In our case we\'re appending a new crumb to the crumbs array.\narray_push( $crumbs, array(\n    \'slug\' => \'search\',\n    \'url\' => \'https://.../search/\',\n) );\n\n$ingredients = array(\n    \'crumbs\' => $crumbs,\n);\n\nthe_bread( $ingredients );\n
Run Code Online (Sandbox Code Playgroud)\n

HTML5结构输出

\n
<ol class=" bread" itemscope="" itemtype="https://schema.org/BreadcrumbList">\n    <li class="crumb" itemprop="itemListElement" itemscope="" itemtype="https://schema.org/ListItem">\n        <a itemprop="item" href="http://example.com/where/">\n            <span itemprop="name">Where</span>\n        </a>\n        <meta itemprop="position" content="1">\n    </li>\n    >\n    <li class="crumb" itemprop="itemListElement" itemscope="" itemtype="https://schema.org/ListItem">\n        <a itemprop="item" href="http://example.com/where/is/">\n            <span itemprop="name">Is</span>\n        </a>\n        <meta itemprop="position" content="2">\n    </li>\n    >\n    <li class="crumb" itemprop="itemListElement" itemscope="" itemtype="https://schema.org/ListItem">\n        <a itemprop="item" href="http://example.com/where/is/my/">\n            <span itemprop="name">My</span>\n        </a>\n        <meta itemprop="position" content="3">\n    </li>         \n    >\n    <li class="crumb" itemprop="itemListElement" itemscope="" itemtype="https://schema.org/ListItem">\n        <a itemprop="item" href="http://example.com/where/is/my/bread/">\n            <span itemprop="name">Bread</span>\n        </a>\n        <meta itemprop="position" content="4">\n    </li>\n</ol>\n
Run Code Online (Sandbox Code Playgroud)\n

最小 CSS 样板(可选)

\n
.,\n.bread {\n  list-style-type: none;\n  margin:0;\n  padding:0;\n}\n\n. li,\n.bread li {\n  display:inline-block;\n}\n\n. li.crumb:last-child a,\n.bread li.crumb:last-child a {\n  text-decoration: none;\n  pointer-events: none;\n  color: inherit;\n}\n
Run Code Online (Sandbox Code Playgroud)\n

回收碎屑

\n

即使我们建议您使用the_bread()函数来显示和构建您自己的面包屑,您也可以使用它get_the_crumbs()来检索面包屑对象。

\n

示例:输出 crumbs 对象

\n
.,\n.bread {\n  list-style-type: none;\n  margin:0;\n  padding:0;\n}\n\n. li,\n.bread li {\n  display:inline-block;\n}\n\n. li.crumb:last-child a,\n.bread li.crumb:last-child a {\n  text-decoration: none;\n  pointer-events: none;\n  color: inherit;\n}\n
Run Code Online (Sandbox Code Playgroud)\n


Nab*_*han 9

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

您可以调查它们并根据需要对其进行修改!

希望对您有所帮助!

  • 应避免仅链接的答案。 (4认同)
  • 第一个工作顺利。额外的好处 - 它很好而且紧凑,不会做那种令人恼火的间隔代码的事情。 (3认同)

归档时间:

查看次数:

7423 次

最近记录:

7 年,4 月 前