gutenberg icon indicating copy to clipboard operation
gutenberg copied to clipboard

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

Open getdave opened this issue 5 years ago • 27 comments

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.

getdave avatar May 04 '21 09:05 getdave

@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

gwwar avatar Oct 11 '21 17:10 gwwar

Yup, thanks for double checking, @gwwar !

priethor avatar Oct 11 '21 17:10 priethor

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

mgratch avatar Nov 07 '22 21:11 mgratch

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.

Humanify-nl avatar Nov 27 '22 09:11 Humanify-nl

+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

Odinnh avatar Mar 07 '23 10:03 Odinnh

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

getdave avatar Mar 07 '23 10:03 getdave

@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

priethor avatar Mar 19 '23 13:03 priethor

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

getdave avatar Mar 20 '23 09:03 getdave

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' );
		}
	}
}

cr0ybot avatar May 08 '23 01:05 cr0ybot

@cr0ybot Thanks for sharing your code.

Humanify-nl avatar May 08 '23 07:05 Humanify-nl

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

masteradhoc avatar Jul 05 '23 14:07 masteradhoc

+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.

wpchannel avatar Aug 12 '23 08:08 wpchannel

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.

Humanify-nl avatar Aug 12 '23 10:08 Humanify-nl

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.

getdave avatar Aug 14 '23 10:08 getdave

+1, definitely needed!

walton-alex avatar Nov 22 '23 10:11 walton-alex

+1

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

JeffersonReal avatar Dec 08 '23 00:12 JeffersonReal

+1 Running into this same issue

nathan-schmidt-viget avatar Jan 05 '24 21:01 nathan-schmidt-viget

+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.

ltrihan avatar Feb 18 '24 23:02 ltrihan

+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.

uladzimirkulesh avatar Jun 02 '24 21:06 uladzimirkulesh

I had a similar issue with missing the 'active' class on menu items because they weren't available to add in the Menus screen. Based on @cr0ybot 's JS, I adjusted the function a bit so that it's just based on path. The current-menu-item class gets added to a menu list item if it's path matches the browser path.

This fixed my initial issue and also actually helped out on a separate header navigation/project where I always needed the parent menu item 'active' if on a 'child' post/page.

function setCurrentMenuItemClass() {
    // Get the current URL.
    const currentPath = window.location.pathname;
    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;
        }
    }

    menuItems.forEach(menuItem => {
        const link = menuItem.querySelector( 'a' );
        const linkURL = link.pathname;

        // If the current browser path includes the menu list item path, add the class.
        if ( currentPath.includes(linkURL) ) {
            menuItem.classList.add( 'current-menu-item' );
        }
    });
}

laurelstreng avatar Nov 04 '24 21:11 laurelstreng