[Condition] Menu Category Taxonomy Term

le 26/10/2021
JAG

This members-only tutorial for Oxygen provides the steps to register a custom condition using which menus can be shown conditionally on Pages based on “Menu Category” taxonomy selected in the Page edit screens.
Page Edit Screen

This condition is meant to be used for the Template that applies to all static Pages or when editing individual Pages in Oxygen editor.
The selected menu from the list of dynamically generated WP menus will be checked against the menu category set for that Page.
Use Case
Add several Menu components for the various Pages (Ex.: “About Menu” for all about Pages, “Services Menu” for all service pages) and this condition will enable you to load the corresponding menu.
Step 1
Install and activate Custom Post Type UI plugin.
Go to CPT UI > Add/Edit Taxonomies.
Add a new taxonomy like so:

Set Hierarchical to True so it behaves like categories with checkboxes.
Step 2
Install and activate Code Snippets plugin.
Go to Snippets > Add New.
Title: [Condition] Menu Category for Pages
Code:

// get IDs of all client menus.
$menus = get_terms( 'nav_menu', array( 'hide_empty' => true ) );

// define an empty array to store the names of all menus.
$menu_names = array();

// populate the menu names array.
foreach ( $menus as $menu ) {
$menu_names[] = wp_get_nav_menu_object( $menu )->name;
}

if ( function_exists( 'oxygen_vsb_register_condition' ) ) {

global $oxy_condition_operators;

oxygen_vsb_register_condition(
// Condition Name
'Menu Category',

// Values: The array of pre-set values the user can choose from.
// Set the custom key's value to true to allow users to input custom values.
array(
'options' => $menu_names,
'custom' => false
),

// Operators
array( '==' ),

// Callback Function: Name of function that will be used to handle the condition
'custom_condition_menu_category',

// Condition Category: Default ones are Archive, Author, Other, Post, User
'Page'
);

}

/**
* Callback function to handle the condition.
* @param mixed $value Input value - in this case, name of "Menu Category" custom taxonomy term.
* @param string $operator Comparison operator selected by the user.
*
* @return boolean true or false.
*/
function custom_condition_menu_category( $value, $operator ) {

// if we are not on a static Page, abort.
if ( ! is_page() ) {
return;
}

// get all the menu_category taxonomy terms for the current Page.
$terms = get_the_terms( get_the_ID(), 'menu_category' );

// store the first term name in a variable.
if ( $terms && ! is_wp_error( $terms ) ) {

$page_terms = array();

foreach ( $terms as $term ) {
$page_terms[] = $term->name;
}

$page_term = $page_terms[0];
}

// if the first term of menu_category for current Page is the value selected by user in the condition, return true else false.
return ( $value === $page_term ) ? true : false;

}
Set it to run everywhere.
Save changes and activate.
Step 3
At Appearance > Menus if you have not already, create different menus for different groups of Pages.
Edit your Pages and enter the name of menu in the Menu Categories metabox for each.
Ensure that name matches exactly with what’s present at Appearance > Menus.

Note: If more than one is entered/selected, only the first one will be used.
Step 4
Edit the Oxygen Template that applies to all Pages.
Add a Menu component.
Select the menu that should appear. Ex.: About Menu.
Set Menu Layout to Vertical.
Make any other styling adjustments as needed.
Click on the Condition icon and “Set Conditions” button.
Locate the “Menu Category” condition under “Page” in the left dropdown.
In the right dropdown select the corresponding WordPress menu.
This Menu component will be printed (outputted) on the front end only for those Pages that have the corresponding “Menu Category” ticked.
Repeat for other Menu components.
References
https://wordpress.stackexchange.com/a/264260/14380
https://stackoverflow.com/a/3045647/778809

get_the_terms()

Nos experts se tiennent à votre disposition
pour répondre à vos questions et vous conseiller
dans votre projet digital.

Vous souhaitez développer une application ?

Envie d’échanger avec l’un de nos experts ? Besoin d’en savoir plus ?
En savoir plus

Recevez notre newsletter

La protection des données nous tient à cœur. JAG utilise les informations que vous fournissez afin de vous proposer des informations et du contenu pertinent sur nos produits et services. Vous pouvez vous désinscrire de ce type de communications à tout moment. Pour plus d'informations, consultez notre politique de confidentialité
© 2022 JAG Consulting, sas. Tous droits réservés.
© 2022 JAG Consulting, sas. Tous droits réservés.
4.8
Lire les 51 avis
google
Brieuc Pauly
08/09/2022

Créative, dynamique et très professionnelle, c'est à mon sens les piliers qui soutiennent cette équipe talentueuse ! Une agence à taille humaine qui privilégie la qualité et le résultat. Alors si vous souhaitez le meilleur pour votre communication adressez vous les yeux fermés à ces passionnés, plus que compétents ! (Translated by Google) Creative, dynamic and very professional, these are in my opinion the pillars that support this talented team! A human-sized agency that favors quality and results. So if you want the best for your communication, turn your eyes closed to these enthusiasts, who are more than competent!

google
Amaury Tardier
08/09/2022

Merci à Julien et son équipe, plus que satisfait par le travail fourni ! Je recommande vivement ! (Translated by Google) Thanks to Julien and his team, more than satisfied with the work provided! I highly recommend !

Ravi de travailler avec l'équipe JAG pour la gestion de mon site de e-Commerce. Equipe réactive et toujours pleine de bonnes idées pour améliorer les performances du site (Translated by Google) Glad to work with the JAG team for the management of my e-Commerce site. Responsive team always full of good ideas to improve the performance of the site

Merci à Julien pour son accompagnement dans mon projet, je recommande à 200% c est le Top (Translated by Google) Thanks to Julien for his support in my project, I recommend 200% it is the top

Excellente agence. Equipe est très pro, à l'écoute et de bon conseils. (Translated by Google) Excellent agency. The team is very professional, attentive and gives good advice.

Temps de réponse hors pair quand il y a eu un pb sur mon site, je vous recommande chaleureusement Julien :-) (Translated by Google) Unparalleled response time when there was a problem on my site, I warmly recommend Julien :-)

Charger plus
listarrow-right
Julien

Julien

Votre conseiller

I will be back soon

Une question ?

Julien
Bonjour ! Je suis Julien.
Comment puis-je vous aider?
Démarrer la conversation:
chat Live Chat