15th Feb '24
/
0 comments

Filtering Breadcrumbs in Bricks

In the Bricks Facebook group a user asks:

Hi! Is there a PHP filter to customize the Bricks breadcrumbs? I want to replace /Uncategorized/ with a link to another page (not an archive).

Bricks has a bricks/breadcrumbs/items filter (undocumented as of today) that can be used to modify the breadcrumbs’ output programmatically.

For example,

add_filter( 'bricks/breadcrumbs/items', function ( array $breadcrumb_items ): array {
  if ( ! is_singular( 'post' ) ) {
    return $breadcrumb_items;
  }

  if ( isset( $breadcrumb_items[1] ) ) {
    $breadcrumb_items[1] = str_replace( 'category/uncategorized/', '', $breadcrumb_items[1] );
  }
  
  return $breadcrumb_items;
});

will replace all instances of category/uncategorized/ in the breadcrumbs on single posts in the front end with an empty string thereby effectively linking Uncategorized to the site’s homepage.

If you are using the BricksExtras’ Site Breadcrumbs element instead (you should, because it has more configurable options) just change the filter name to bricksextras/breadcrumbs/.

add_filter( 'bricksextras/breadcrumbs/', function ( array $breadcrumbsFlat ): array {
  if ( ! is_singular( 'post' ) ) {
    return $breadcrumbsFlat;
  }

  if ( isset( $breadcrumbsFlat[1] ) ) {
    $breadcrumbsFlat[1] = str_replace( 'category/uncategorized/', '', $breadcrumbsFlat[1] );
  }
  
  return $breadcrumbsFlat;
});
Get access to all 610 Bricks code tutorials with BricksLabs Pro

Leave the first comment

 

Related Tutorials..

Pro
Conditionally Excluding Elements from Bricks Editor

Conditionally Excluding Elements from Bricks Editor

In the past, we shared How to Exclude Elements from Bricks Editor. This Pro tutorial shows how specific elements can be excluded selectively based on…
How to Remove Name and Website Fields in Bricks Comment Forms

How to Remove Name and Website Fields in Bricks Comment Forms

Updated on 14 Mar 2024 In the BricksLabs Facebook group a user asked: What is the best method to remove fields from the Bricks "Comments"…
Pro
How to Inject a Different Type of Post in Bricks Query Loop

How to Inject a Different Type of Post in Bricks Query Loop

Updated on 26 Aug 2024 A user asks: Hello, I'd like to inject a CPT post card inside of a Bricks query loop of a different CPT…
Pro
Prefiltering Bricks Terms Query Loop

Prefiltering Bricks Terms Query Loop

Let's say there's a Events CPT with a 'Event Year' custom taxonomy. Sample event year names could be: 2021, 2022, 2024, 2025, 2028 etc. We…
Pro
Programmatically populating ACF field values in WordPress

Programmatically populating ACF field values in WordPress

An example of how to set the values of a Select-type field with the names and labels of all public post types.
Categories:
How to Exclude Elements from Bricks Editor

How to Exclude Elements from Bricks Editor

Bricks has a handy bricks/builder/elements filter using which the available elements that show up after clicking + (or ⌃/⌘ + ⇧ + E) in the…
Categories:
Tags: