Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add filter for label text #6

Closed
wants to merge 1 commit into from
Closed

add filter for label text #6

wants to merge 1 commit into from

Conversation

daverobertson
Copy link

Allows overriding the breadcrumb labels within a theme (for single pages/posts).

In our use case, a client has pages with long titles down the hierarchy. We added an ACF custom field named short_title so a page title like "This a very long title about this cool topic" could have an accompanying short title of "Cool Topic". The latter would be used by the theme where space is limited (ie, breadcrumbs).

This PR adds a log1x/crumb_label filter to allow the theme to override the crumb's labels, such as:

// (in functions.php)
// Replace the crumb's label when short_title is set for a page/post:
add_filter('log1x/crumb_label', function ($title, $post) {
    return get_field('short_title', $post) ?? $title;
}, 10, 2);

Notes:

  • Instances of get_the_title() are replaced by a new getPostTitle() method just to keep things DRY.
  • This only handles titles for posts, but maybe it inspires ideas for other views like archives, etc.

allows overriding breadcrumb labels within a theme
@Log1x
Copy link
Owner

Log1x commented Mar 27, 2022

Is there another use-case for a filter in particular?

In this case, I'd think {{ get_field('short_title', $post) ?? $item->label }} would make more sense.

@daverobertson
Copy link
Author

Yeah that would work if the Post ID of each "crumb" was available in the returned array, then we could do:

{{ get_field('short_title', $item->id) ?? $item->label }}

...but since each $item only has a label and url, a filter for the label does the job. With $item->id your alternative feels clean and could open more possibilities for overriding output in view composers, etc... 🤔 what do you think?

Log1x added a commit that referenced this pull request Jul 29, 2023
Log1x added a commit that referenced this pull request Jul 29, 2023
…e (Supersedes #6) (#11)

* chore(deps): Bump dev-dependencies
* fix(docs): Fix build status badge
* chore(ci): Test against 8.1 and 8.2
* chore(ci): Drop test against 7.3
@Log1x
Copy link
Owner

Log1x commented Jul 29, 2023

ID's have been added for v1.0.2

@Log1x Log1x closed this Jul 29, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants