Breadcrumbs Support #149
|
Hi, this project looks interesting. Thanks for creating it. I have a question. Let's say I have a page structure with these breadcrumbs:
There is an issue, where if I update the title of the breadcrumbs are unchanged
I want it to show
This is because an update to I looked into adding Is there a recommended way to do this? Ideally something robust that is a light touch to the codebase. Could there be a function like |
Replies: 2 comments 2 replies
|
@earthlingdavey Do you mean that 'Post 2' is a child post of 'Post 1'? Do you use the hierarchy feature for pages here? |
|
Thank you so much, @earthlingdavey! This is exactly the kind of situation where one of MilliCache's main features shines: Rules. It wouldn't make sense to bake a fix into MilliCache itself as that would purge every user's parent pages regardless of whether they actually mention the parent title (e.g. in a breadcrumb). In your case it's easy to solve with a custom rule. Add the ancestor The child depends on the parent, so the dependency belongs on the child's cache entry, declared only when the crumb is actually rendered. That's exactly the condition → action shape rules give you:
Nothing is baked into core, nothing clears a parent that has no dependents, and it's fully opt-in per site. millicache()->rules()->create( 'my-rule:breadcrumbs' )
->on( 'template_redirect', 25 )
->when()
// Pick whatever identifies your crumb-bearing pages. These are AND-ed,
// so use ->when_any() instead if you mean "either/or".
->is_singular()
// In WP >6.9 there is a breadcrumbs block
->has_block( 'core/breadcrumbs' )
->then()
->custom( 'add-crumb-flags', function () {
foreach ( get_post_ancestors( get_queried_object_id() ) as $ancestor_id ) {
millicache()->flags()->add( 'post:' . $ancestor_id );
}
} )
->register();Now the child additionally carries its parent's (and grandparent's) Also clearing crumb pages when the Home title changes
If Home is a static front page, just add one more dependency in the same action — the front-page post itself: ->custom( 'add-crumb-flags', function () {
// Parent / grandparent crumbs
foreach ( get_post_ancestors( get_queried_object_id() ) as $ancestor_id ) {
millicache()->flags()->add( 'post:' . $ancestor_id );
}
// Home crumb → depend on the front-page post itself
$front_id = (int) get_option( 'page_on_front' );
if ( $front_id ) {
millicache()->flags()->add( 'post:' . $front_id );
}
} )Now editing the front page clears One thing to note: because this rides on MilliCache's automatic post-update clearing, those pages are purged (rebuilt cold on the next request), not expired. That's perfectly fine for most sites. If you'd rather serve stale-while-revalidate during the rebuild, you'd flip it around to an invalidation-side rule that clears with There are many ways to solve this. You could also come from the other side and invalidate A good starting point for working with Rules is in the MilliCache docs and you can deep-dive in the MilliRules docs, too. |
Thank you so much, @earthlingdavey! This is exactly the kind of situation where one of MilliCache's main features shines: Rules.
It wouldn't make sense to bake a fix into MilliCache itself as that would purge every user's parent pages regardless of whether they actually mention the parent title (e.g. in a breadcrumb). In your case it's easy to solve with a custom rule.
Add the ancestor
post:flag to childrenThe child depends on the parent, so the dependency belongs on the child's cache entry, declared only when the crumb is actually rendered. That's exactly the condition → action shape rules give you:
p…