• Resolved Mart89

    (@mart89)


    Hi, a while back i found the code below on a forum.

    It does exactly what i would like it to do; it displays a menu made up of a custom taxonomy without any anchors, in which a ‘current’ class gets added to the list items when applicable.

    The problem is that this code doesnt give individual classes to each list item so i cant style them individually. Does anyone know how i might add them?

    <?php
    	 // Get terms for post
    	 $currentterm = get_the_terms( $post->ID , 'product-types' );
    	 // Loop over each item since it's an array
    	 if ( $currentterm != null ){
    	 foreach( $currentterm as $current ) {
    	} }
    
    	$terms = get_terms('product-types', array(
    	 	'orderby'    => 'ID',
    	 )); 
    
    	foreach ($terms as $term) {
    	$class = $current->slug == $term->slug ? 'current' : 'notCurrent' ;
    	echo '<li class="'. $class .'">' . $term->slug . '</li>'; 	}
    
    	unset($current);
    ?>
Viewing 6 replies - 1 through 6 (of 6 total)
  • The key is here

    echo '<li class="'. $class .'">' . $term->slug . '</li>';

    This is what we call PHP concatenation. You can see more info on it here: https://php.net/manual/en/language.operators.string.php

    Basically the code is just echoing a string. So, if you want to add a class to each li element, you need only do the following:

    echo '<li class="someAmazingClassHere '. $class .'">' . $term->slug . '</li>';

    Notice I left a space after the class, that’s because your current class will be appended inside there based on the PHP var.

    Moderator bcworkz

    (@bcworkz)

    If you want a different class in each iteration of the loop, you could concatenate a number to $class that is incremented in each iteration.

    $i = 1;
    foreach ($terms as $term) {
       $class = $current->slug == $term->slug ? 'current' : 'notCurrent' ;
       $class .= $i;
       echo '<li class="'. $class .'">' . $term->slug . '</li>';
       $i++;
    }

    Thread Starter Mart89

    (@mart89)

    Thanks guys that was so incredibly helpful. You’ve saved my ass!

    For anyone else interested i made a slight addition to the code above from bcworkz, so that it each list-item would have the current or notCurrent class followed by ‘item1’, ‘item2’ etc.

    <?php
    	 // Get terms for post
    	 $currentterm = get_the_terms( $post->ID , 'product-types' );
    	 // Loop over each item since it's an array
    	 if ( $currentterm != null ){
    	 foreach( $currentterm as $current ) {
    	} }
    
    	$terms = get_terms('product-types', array(
    	 	'orderby'    => 'ID',
    	 )); 
    
    	$i = 1;
    	foreach ($terms as $term) {
    	   $class = $current->slug == $term->slug ? 'current' : 'notCurrent' ;
    	   $class .=  ' item';
    	   $class .=  $i;
    	   echo '<li class="'. $class .'">' . $term->slug . '</li>';
    	   $i++;
    	}
    	unset($current);
    ?>
    Thread Starter Mart89

    (@mart89)

    I do have a follow-up question if anyone can help.

    This code brings up an error on pages where the ‘product-types’ term doesnt exist. I guess i need to put in something that checks for this term before it runs the code?

    Moderator bcworkz

    (@bcworkz)

    Yes you do. Something like what was done with $currentterm should work. Wrap everything I gave you previously with something like

    if ( $terms != null ){
       // previous code here
    }

    Generally speaking, PHP is particular about trying to use variables that either are undefined or have no usable value. Any time there’s any chance of this you need to do protective checks like != null, isset(), array_key_exists(), etc., depending on the specific situation.

    If there’s any chance the taxonomy does not exist, you should also check && ! is_wp_error( $terms )

    Thread Starter Mart89

    (@mart89)

    That’s brilliant, thanks once again!

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Assigning individul post Id's to menu items’ is closed to new replies.