• Resolved paa1605

    (@paa1605)


    Hi guys,

    I have a site selling sports equipment. I have a taxonomy named ‘Brands’, and in that taxonomy terms such as nike, adidas etc. Im trying to create a page for my website of brands listed a-z. The list will simply be the taxonomy term, and i don’t want these terms to be links, purely a list for reference purposes.

    I don’t have a brand representing every letter of the alphabet, so the code needs to check if there are any brands and then display the name accordingly. Does anyone know how to do this??

    I found this on a similar post that works but relates to tags. If anyone can amend the code so it works for the taxonomy terms i would be very grateful. Thanks, Patrick.

    <?php
    	$list = '';
    	$tags = get_terms('your_custom_taxonomy');
    	$groups = array();
    
    	if( $tags && is_array( $tags ) ) {
    		foreach( $tags as $tag ) {
    			$first_letter = strtoupper( $tag->name[0] );
    			$groups[ $first_letter ][] = $tag;
    		}
    
    		if( !empty( $groups ) ) {
    				{
    				$index_row .='<ul class="topindex">';
    				foreach ($groups as $letter => $tags) {
    					$index_row .= '
    <li><h4><a rel="nofollow" href="#' . $letter . '-folder" title="' . $letter . ' folder">' . apply_filters( 'the_title', $letter ) . '</a></h4></li>
    ';
    				}
    
    				$index_row .='<br class="clear">';
    			}
    
    			$list .= '<ul class="index">';
    			foreach( $groups as $letter => $tags ) {
    				$list .= '
    <li><a></a><h5><a rel="nofollow" href="#tags_top" title="Back to index">' . apply_filters( 'the_title', $letter ) . '</a></h5>';
    				$list .= '<ul class="links">';
    				foreach( $tags as $tag ) {
    					$url = attribute_escape(get_bloginfo('url') . '/' . $tag->taxonomy . '/' . $tag->slug);
    					$name = apply_filters( 'the_title', $tag->name );
    					$list .= '<li class="cat-item"><a rel="nofollow" title="' . $name . ' folder" href="' . $custom_taxonomy_parent . $url . '">' . $name . '</a></li>
    ';
    				}
    
    				$list .= '';
    			}
    
    			$list .= '';
    		}
    
    	} else $list .= '<p>Sorry, but no tags were found</p>';
    	?>
    <a></a>
    <?php  print $index_row; ?>
    <?php  print $list; ?>

Viewing 4 replies - 1 through 4 (of 4 total)
  • Please show an example of what you want the output to look like.

    Thread Starter paa1605

    (@paa1605)

    Here is the page in html looking exactly as i want it. I would rather have it in php so that if i added a new brand name(term) to the taxonomy it updated this page by itself.

    https://yfrog.com/9gscreenshot20101105at101p

    Any help is much appreciated. Thanks.

    Here is the code and css that I used while testing:

    $list = '';
    $args = array(
      'hide_empty' => false  // Set this to true to hide terms with no posts
    );
    $tags = get_terms('madeof',$args);
    $groups = array();
    
    if( $tags && is_array( $tags ) ) {
      foreach( $tags as $tag ) {
        $first_letter = strtoupper( $tag->name[0] );
        $groups[ $first_letter ][] = $tag;
      }
    
      if( !empty( $groups ) ) {
        $index_line = '<p class="header-line">A-Z Brands</p>';
        foreach( $groups as $letter => $tags ) {
          $list .= "\n<p class='letter-line'><span class='first-letter'>" .
            apply_filters( 'the_title', $letter ) . '</span>&nbsp;';
          foreach( $tags as $tag ) {
            $name = apply_filters( 'the_title', $tag->name );
            $list .= '<span class="term-name">' . $name . "</span>&nbsp;";
          }
    
          $list .= '</p>';
        }
    
        $list .= '';
      }
    
    } else $list .= '<p>Sorry, but no tags were found</p>';
    
    //print htmlentities($index_line);
    //print htmlentities($list);
    print ($index_line);
    print ($list);
    /* styles for testing list terms by first letter */
    .header-line {
      font-size: 2.4em;
      color: purple;
    }
    .first-letter {
      background: #efefef;
      font-size: 1.8em;
      margin-right: 10px;
    }
    .term-name {
      margin-right: 20px;
    }
    Thread Starter paa1605

    (@paa1605)

    Thanks Mac, this worked perfectly. However, i’ve slightly changed the site and instead of listing the terms from the ‘brands’ taxonomy in an a-z format, i would like to instead list the sub pages of the ‘brands’ page, again in an a-z format, with wordpress first checking to see if the alphabet letter does have any pages starting with it, and then displaying them in a list as links. I’ve tried to adapt the code above but my php knowledge is quite limited and when i did i got the dreaded blank white screen! If you, or anyone else could help i would be extremely grateful. Thanks,
    Patrick

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘How to list taxonomy terms by first letter?’ is closed to new replies.