WordPress breadcrumb solution without plugins

888u

Last update at :2024-07-02,Edit by888u

I won’t say much about the role of breadcrumb navigation. Anyway, each has its own purpose. I have shared the use of Yoast Breadcrumbs plug-in before. However, as wordpress takes up too much resources, if you can use less plug-ins, you can use plug-ins instead of plug-ins. Just now when Snail was organizing the website, he found this plug-in-free breadcrumb effect which is quite good. I recorded and organized it for future needs.

The first step is to add it to the Functions.php file

function get_breadcrumbs(){global $wp_query;

if ( !is_home() ){

// Start the ULecho \\\'

    \\';// Add the Home linkecho \\\'
  • Website homepage
  • \\';

    if ( is_category() ){$catTitle = single_cat_title( \\”\\”, false );$cat = get_cat_ID( $catTitle );echo \\”

  • » \\”. get_category_parents( $cat , TRUE, \\” » \\” ) .\\”
  • \\”;}elseif ( is_archive() && !is_category() ){echo \\”
  • » Archives
  • \ \";}elseif ( is_search() ) {

    echo \\”

  • » Search Results
  • \\”;}elseif ( is_404() ){echo \\”
  • » 404 Not Found
  • \\”;}elseif ( is_single() ){$category = get_the_category();$category_id = get_cat_ID( $category[0]->cat_name );

    echo \\\'

  • » \\'. get_category_parents( $category_id, TRUE, \\” » \\” );echo the_title(\\'\\',\\'\\', FALSE ) .\\”
  • \\”;}elseif ( is_page() ){$post = $wp_query->get_queried_object();

    if ( $post->post_parent == 0 ){

    echo \\”

  • » \\”.the_title(\\’\\’,\\’\\’, FALSE).\\”
  • \\”;

    } else {$title = the_title(\\'\\',\\'\\', FALSE);$ancestors = array_reverse( get_post_ancestors( $post->ID ) );array_push($ancestors, $post ->ID);

    foreach ( $ancestors as $ancestor ){if( $ancestor != end($ancestors) ){echo \\\'

  • » \\'. strip_tags( apply_filters( \\'single_post_title\\', get_the_title( $ancestor ) ) ) .\\\'
  • \\';} else {echo \\\'
  • » \\'. strip_tags( apply_filters( \\'single_post_title\\', get_the_title( $ancestor ) ) ) .\\\'
  • \\';}}}}

    // End the ULecho \\”

\\”;}}

The second step is to add the breadcrumb plug-in at the appropriate location on the page where you need to add it

The third step, style definition

ul.breadcrumbs {list-style: none;padding: 0;margin: 0;font-size:12px;}ul.breadcrumbs li {float: left;margin: 0 5px 0 0;padding: 0;}< /p>

In summary, through the above three steps, you can achieve the breadcrumb effect without plug-ins, and the style and layout can be modified slightly.

Recommended site search: php virtual space, US php space, mainland China space, php host space, Korean server rental, Chinese domain name, Chinese international domain name, Taiwan server, mainland China registration-free host, Hong Kong vps host,

WordPress breadcrumb solution without plugins

All copyrights belong to 888u unless special state
取消
微信二维码
微信二维码
支付宝二维码