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 Post Type Archive variation to core/navigation-link block #31452

Open
Tracked by #35521
getdave opened this issue May 4, 2021 · 23 comments · May be fixed by #66821
Open
Tracked by #35521

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

getdave opened this issue May 4, 2021 · 23 comments · May be fixed by #66821
Labels
[Block] Navigation Affects the Navigation Block [Type] Enhancement A suggestion for improvement.

Comments

@getdave
Copy link
Contributor

getdave commented May 4, 2021

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
Copy link
Contributor

gwwar commented Oct 11, 2021

@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

@priethor
Copy link
Contributor

Yup, thanks for double checking, @gwwar !

@mgratch
Copy link

mgratch commented Nov 7, 2022

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

@Humanify-nl
Copy link

Humanify-nl commented Nov 27, 2022

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
Copy link

Odinnh commented Mar 7, 2023

+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
Copy link
Contributor Author

getdave commented Mar 7, 2023

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

@priethor
Copy link
Contributor

@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
Copy link
Contributor Author

getdave commented Mar 20, 2023

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

@cr0ybot
Copy link
Contributor

cr0ybot commented May 8, 2023

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
Copy link

@cr0ybot Thanks for sharing your code.

@getdave getdave mentioned this issue May 9, 2023
10 tasks
@masteradhoc
Copy link
Contributor

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

@wpchannel
Copy link

+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
Copy link

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
Copy link
Contributor Author

getdave commented Aug 14, 2023

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.

@jordesign jordesign added the [Type] Enhancement A suggestion for improvement. label Sep 8, 2023
@walton-alex
Copy link

walton-alex commented Nov 22, 2023

+1, definitely needed!

@WebGuyJeff
Copy link

+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
Copy link

+1
Running into this same issue

@ltrihan
Copy link

ltrihan commented Feb 18, 2024

+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
Copy link

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

@laurelstreng
Copy link

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

@getdave
Copy link
Contributor Author

getdave commented Nov 6, 2024

Steps to complete this work:

  • add a new search handler REST API endpoint for post-type-archives
  • update the fetchLinkSuggestions code to include the ability to search archives.
  • apply any updates to the <LinkControl> component (TBD)

This work could be split into PRs for:

  • REST API work
  • fetch handler and component work

Prior art for this in Classic Menus (not using REST API) can be used as a basis for some of the code.

@getdave
Copy link
Contributor Author

getdave commented Nov 7, 2024

I had a PoC for this locally, so I got the ball rolling here with #66821

@Bricobit
Copy link

As of today this still doesn't seem to work and we're reaching 2025, I'm using the latest version of WordPress and the Five theme, without any plugin, and the menus are not selected depending on the page I'm on.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Block] Navigation Affects the Navigation Block [Type] Enhancement A suggestion for improvement.
Projects
None yet
Development

Successfully merging a pull request may close this issue.