PHP code example of jjgrainger / wp-crumbs

1. Go to this page and download the library: Download jjgrainger/wp-crumbs library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

jjgrainger / wp-crumbs example snippets


the_crumbs();

the_crumbs( [
    'home'      => 'Home',   // Title for the Home (front page) link
    'blog'      => 'Blog',   // Title for the blog home page
    'seperator' => '/',      // Seperator to use between each crumb (string or false)
    'class'     => 'crumbs', // The class(es) applied to the wrapper element ('crumbs', 'nav crumbs')
    'element'   => 'nav'     // The HTML element to use (div, nav, ol, ul)
] );

$breadcrumbs = get_crumbs();

print_r( $breadcrumbs );

Array(
    [0] => Array(
        [title] => "Home"
        [url] => "http://site.com/"
    )
    [1] => Array(
        [title] => "Blog"
        [url] => "http://site.com/blog/"
    )
    [2] => Array(
        [title] => "My First Post"
        [url] => false
    )
)


// Example: modify title for a custom post type
function modify_crumbs( $crumbs ) {
    // if on events archive change title to shows
    if ( is_post_type_archive( 'event' ) || is_singular( 'event' ) ) {
        for ( $i = 0; $i < count($crumbs); $i++ ) {
            if ( $crumbs[$i]['title'] === 'Events' ) {
                $crumbs[$i]['title'] = "Shows";
            }
        }
    }

    return $crumbs;
}

add_filter( 'get_crumbs', 'modify_crumbs' );

// Example: add post type archive on taxonomy archive page
function modify_crumbs( $crumbs ) {
    // if on the events category archive page
    if ( is_tax( 'event-categories' ) ) {
        // create a new crumb
        $crumb = [
            'title' => "Shows",
            'url'   => site_url( '/shows' ),
        ];

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

    return $crumbs;
}

add_filter( 'get_crumbs', 'modify_crumbs' );