WordPress / gutenberg

The Block Editor project for WordPress and beyond. Plugin is available from the official repository.
https://wordpress.org/gutenberg/
Other
10.3k stars 4.11k forks source link

Add Post Type Archive variation to `core/navigation-link` block #31452

Open getdave opened 3 years ago

getdave commented 3 years ago

What problem does this address?

Currently you cannot add a Post Type Archive link to the Navigation block. However you can do this via the currently Menus screen.

Here's how you add a Post Type Archive to a menu.

What is your proposed solution?

Add a post-type-archive block variation for the core/navigation-link block. We'll also need to add test coverage for this in the navigation editor.

gwwar commented 2 years ago

@priethor I'm not sure this one needs to be marked as high. With a lighter nav experience in mind, https://github.com/WordPress/gutenberg/issues/34041 folks have been chatting about de-emphasizing link variations in the quick inserter https://github.com/WordPress/gutenberg/pull/35056 and have also chatted about a bulk inserter option in https://github.com/WordPress/gutenberg/issues/31667

priethor commented 2 years ago

Yup, thanks for double checking, @gwwar !

mgratch commented 1 year ago

+1 This issue is... not fun to work around. Thanks!

Humanify-nl commented 1 year ago

This issue is the one reason I still have to remove archives and use normal pages instead,(1) the navigation-block doesn't recognize them. (2) the 'is-active' class is not placed on it, making standard navigation UX totally impossible.

Not fun indeed. Such a standard and crucial feature that still doesn't work after 1 year of FSE.

Odinnh commented 1 year ago

+1 This should be considered a core aspect of the nav block,

we have categories and custom taxonomies, we dont have a user friendly way of adding an archive page.

@priethor I'm not sure this one needs to be marked as high. With a lighter nav experience in mind, #34041 folks have been chatting about de-emphasizing link variations in the quick inserter #35056 and have also chatted about a bulk inserter option in #31667

IMO, it should be high priority seeing as this should be one of the fundamentals of the nav

getdave commented 1 year ago

@scruffian @priethor Should we look to pull this into the scope of work for 6.3? Seems achievable.

priethor commented 1 year ago

@getdave I think the Nav block experience is in a stage where we can re-prioritize this kind of issues; it seems a good thing to tackle for 6.3

getdave commented 1 year ago

Added to High priority on tracking issue for consideration for 6.3 cycle.

cr0ybot commented 1 year ago

As a stop-gap, I wrote this quick and dirty JS function to add current-menu-item to any navigation block item that matches the current URL. There are probably a lot of edge cases it won't account for, but it's working ok for me (for now).

Maybe there is a better way to do it server-side, but I'm hoping this issue with the navigation block is resolved before I feel the need to figure that out...

/**
 * Set current-menu-item class on link that matches current URL.
 */
export function setCurrentMenuItemClass() {
    const menuItems = document.querySelectorAll( '.wp-block-navigation-item' );

    if ( ! menuItems.length ) {
        return;
    }

    // Check for .current-menu-item class on any item and stop if found.
    for ( let i = 0; i < menuItems.length; i++ ) {
        if ( menuItems[ i ].classList.contains( 'current-menu-item' ) ) {
            return;
        }
    }

    // Add trailing slash to path if missing.
    const url = window.location.href.endsWith( '/' )
        ? window.location.href
        : `${ window.location.href }/`;

    // Check for matching URL path on any child link of menuItems.
    for ( let i = 0; i < menuItems.length; i++ ) {
        const link = menuItems[ i ].querySelector( 'a' );
        const linkURL = link.href.endsWith( '/' )
            ? link.href
            : `${ link.href }/`;

        // Note: link.href returns full URL, even if it's a relative link.
        if ( linkURL === url ) {
            menuItems[ i ].classList.add( 'current-menu-item' );
        }
    }
}
Humanify-nl commented 1 year ago

@cr0ybot Thanks for sharing your code.

masteradhoc commented 1 year ago

+1 on this one, definately needed. just have to build custom links now for this.

wpchannel commented 1 year ago

+1 this is missing! I'm currently moving from custom theme to block theme and migrating menus is failing on this kind of links too.

Humanify-nl commented 1 year ago

Added to High priority on tracking issue for consideration for 6.3 cycle.

Did this get picked up for 6.3? Didn’t see anything in the notes.

getdave commented 1 year ago

It didn't get address in the 6.3 cycle unfortunately.

Contributors are looking at priorities for 6.4 now based on capacity. As always PRs are always welcomed.

walton-alex commented 9 months ago

+1, definitely needed!

WebGuyJeff commented 9 months ago

+1

Just ran into this. I have a few CPTs which need the archive/child pages to be menu item/sub-menu items respectively.

nathan-schmidt-viget commented 8 months ago

+1 Running into this same issue

ltrihan commented 6 months ago

+1 People keep telling me about using the template hierarchy instead of page templates 😛 , but it's true that it's annoying not to have these little features natively.

uladzimirkulesh commented 3 months ago

+1

Just ran into this. I have a few CPTs which need the archive/child pages to be menu item/sub-menu items respectively.

I have the same problem.