• Resolved Uladzimir Kulesh

    (@uladzimirkulesh)


    Hello! Please tell me how to add the current-menu-item class to a menu item if the specified page is active? I wrote a portfolio plugin that adds a new post type to the site – a project. All projects from it are displayed on the page /portfolio (I use ‘rewrite’ => array( ‘slug’ => ‘portfolio’ )). Previously, before the WordPress editor was extended to full site editing, the specified class was automatically added to the menu item when visiting the portfolio page (with a list of all projects). Now this doesn’t happen. Moreover, nothing can be done using the options offered on the Internet. Most likely, because they were not written for the current version of WordPress.

    Please tell me how can I do this now? For all pages owned by WordPress itself, the current class is added to the menu items.

Viewing 9 replies - 1 through 9 (of 9 total)
  • threadi

    (@threadi)

    The question is rather which theme are you using? This is responsible for the menu output in the frontend.

    Thread Starter Uladzimir Kulesh

    (@uladzimirkulesh)

    @threadi I’m developing my own theme, but I understand that this would be true for any block theme. The Twenty Twenty-Four theme has the same problem. Here’s the plugin I’m using: https://wordpress.org/plugins/uk-portfolio/. And a screenshot:

    Link to image: https://uladzimirkulesh.com/screenshot.png

    threadi

    (@threadi)

    This information is crucial. How a block theme creates a menu is quite different from a classic theme.

    In the current standard theme TwentyTwentyFour, the current menu item is marked with the class “current-menu-item”, see: https://imgur.com/a/R62Z4vW

    If this does not currently happen with your theme, the question would be how you have defined the menu in it? TwentyTwentyFour uses the navigation block that provides this marking.

    However, I also see an open issue on the Gutenberg theme: https://github.com/WordPress/gutenberg/issues/42299 – this may also help you, as it contains information on theme.json.

    Thread Starter Uladzimir Kulesh

    (@uladzimirkulesh)

    @threadi : “If this does not currently happen with your theme, the question would be how you have defined the menu in it? TwentyTwentyFour uses the navigation block that provides this marking.”

    I also use the navigation block. The problem is not in the file theme.json, but in the fact that for a custom post type in the menu, adding a class “current-menu-item” does not work.

    threadi

    (@threadi)

    I have tested this with one of my plugins. There, when I link an entry of the post type in the menu, “current-menu-item” is set at the menu item. The same happens with the post type of your plugin. It actually fits as it should.

    But, however: your plugin does not seem to reset the permalink cache itself. If you install the plugin, then create a portfolio entry and want to view it, you end up on a 404 page. Of course, this cannot be marked as active in the menu. Only manually clearing the permalinks cache solves the problem.

    Thread Starter Uladzimir Kulesh

    (@uladzimirkulesh)

    @threadi If you pin the project page to the menu, then yes, everything works. But I’m talking about the archive page – a page with a list of all projects:

    “All projects from it are displayed on the page /portfolio (I use ‘rewrite’ => array( ‘slug’ => ‘portfolio’ )). Previously, before the WordPress editor was extended to full site editing, the specified class was automatically added to the menu item when visiting the portfolio page (with a list of all projects).”

    Thread Starter Uladzimir Kulesh

    (@uladzimirkulesh)

    @threadi : “But, however: your plugin does not seem to reset the permalink cache itself. If you install the plugin, then create a portfolio entry and want to view it, you end up on a 404 page.”

    Can you tell me where I can read about how to clear the cache when installing the plugin?

    threadi

    (@threadi)

    I think I can see the problem now. If you want to create a link to your archive page in the menu, this is currently not possible except via Custom Link and the manual specification of a URL. This means that the menu item is not linked to a page that WordPress recognizes. This in turn means that it cannot be marked as active.

    The Gutenberg team still has an open issue about this: https://github.com/WordPress/gutenberg/issues/31452 – someone has also posted a JS solution there. But a clean solution is still missing. If you have a need for this, I would recommend that you give this issue a higher priority with your comment there.

    To clear the permalink cache, you should set an option or transient when installing and updating your plugin. In an init hook, you then only call the flush_rewrite_rules() function if the option or transient is set. You can find examples of this here: https://wordpress.stackexchange.com/questions/210126/best-way-to-flush-rewrite-rules-for-custom-post-type-in-a-mu-plugins-plugin

    The forum also uses the block editor for posts. Here you can also use the Quote Block to make quotes much easier to read. See: https://wordpress.org/support/forum-user-guide/block-editor/#quote-block

    Thread Starter Uladzimir Kulesh

    (@uladzimirkulesh)

    @threadi thanks for your help!

Viewing 9 replies - 1 through 9 (of 9 total)
  • You must be logged in to reply to this topic.