• Resolved SpencerCE89

    (@spencerce89)


    I’m using the following code to get a list of my terms from a custom taxonomy:

    <?php
    
    $terms = get_terms("character");
    $count = count($terms);
    if ( $count > 0 ){
    echo "<ul>";
    foreach ( $terms as $term ) {
    echo '<li><a href="' . get_term_link($term) . '">'. $term->name . '</a></li>';
    
    }
    echo "</ul>";
    }
    
    ?>

    Is there a way to only pull up results that start with the same letter?

Viewing 12 replies - 1 through 12 (of 12 total)
  • How do you want to set the ‘same letter’? Once you have the letter, you could use an if statement to only echo if the $term->name starts with that letter. If $letter contains the letter, use something like this:

    if ( $count > 0 ){
       $ul = "<ul>";
       foreach ( $terms as $term ) {
          if ( preg_match("/^$letter/i",$term->name) ) {
             echo $ul;
             $ul = '';  // only display once
             echo '<li><a href="' . get_term_link($term) . '">'. $term->name . '</a></li>';
          }
       }
       if ( $ul == '') {
          echo "</ul>";
       }
    }
    Thread Starter SpencerCE89

    (@spencerce89)

    Thanks for the reply.

    What do you mean by “how do you want to set the same letter?”

    I’m a total newbie when it comes to this, any time I get anything to work it’s usually by luck, and not by any real experience with what I’m doing.

    You said you want to show all terms with the same letter. What is the letter? How do you pick the letter?

    Thread Starter SpencerCE89

    (@spencerce89)

    Ah, I get you.

    Assuming it’s possible, I’d be creating 26 separate pages, one for each letter of the alphabet. I’d be using the same code for each page, with a different letter for each one.

    Thread Starter SpencerCE89

    (@spencerce89)

    Wondering if perhaps I misunderstood the question again. I’m unsure of what code I would use to set the letter, if that’s what you meant. That’s what I’m trying to determine here. I’ve looked a bit at the conditional tags page of the WP codex but it’s way above my head. You think it would be possible to accomplish what I’m looking for, though?

    Where is the code you showed? Is it in a template that can be assigned to a Page?

    If so, you could create a Custom Field for each Page and give it the value of the letter for that Page. Then you could use get_post_meta() to get the letter.

    Thread Starter SpencerCE89

    (@spencerce89)

    Yes, right now it’s in a template for a page called “page-characters.php”, which brings up every term from the taxonomy. I edit the theme file itself instead of entering content via the wordpress admin section. I’d do the same for the themes for each letter. “page-a.php”, “page-b.php”, “page-c.php” etc etc.

    Could you give me an example of how this would be implemented into my code?

    If you are going to have a separate template for each page, just set the letter in the code, like this:

    $letter = 'A';
    if ( $count > 0 ){
       $ul = "<ul>";
    Thread Starter SpencerCE89

    (@spencerce89)

    This is what I have now placed in the theme:

    <?php
    
    $terms = get_terms("character");
    $count = count($terms);
    $letter = 'A';
    if ( $count > 0 ){
    echo "<ul>";
    foreach ( $terms as $term ) {
    echo '<li><a href="' . get_term_link($term) . '">'. $term->name . '</a></li>';
    
    }
    echo "</ul>";
    }
    
    ?>

    It’s still showing all of the terms. Can you tell me what I’m doing wrong?

    You are not using the code that I gave you. Replace what you have with this:

    <?php
    $terms = get_terms("character");
    $count = count($terms);
    $letter = 'A';
    if ( $count > 0 ){
       $ul = "<ul>";
       foreach ( $terms as $term ) {
          if ( preg_match("/^$letter/i",$term->name) ) {
             echo $ul;
             $ul = '';  // only display once
             echo '<li><a href="' . get_term_link($term) . '">'. $term->name . '</a></li>';
          }
       }
       if ( $ul == '') {
          echo "</ul>";
       }
    }
    ?>
    Thread Starter SpencerCE89

    (@spencerce89)

    Ah, sorry! Didn’t realize that that was supposed to be applied to the previous code you posted. My mistake.

    It appears to work very well. Thank you so much for your patience!

    Hi, I know I’m posting to an old thread, but this was pretty helpful when figuring out how to tack a similar issue. I needed the taxonomy lists to show up on separate pages because they were going to be so long. Each letter containing a minimum of 50 terms.

    I think, however, that creating a page template for 26 letters is a little tedious.

    So I decided to go the form route. Here is what I did:

    Make a form that posts to the current page:

    Make a form that posts to current page.
    
    <p>Browse Categories Alphabetically</p>
    <form action='#' method='post' name='form_filter' class="region">
    	<select name="alphabet">
    		<option value="A">A</option>
    		<option value="B">B</option>
    		<option value="C">C</option>
    		<option value="D">D</option>
    		<option value="E">E</option>
    		<option value="F">F</option>
    		<option value="G">G</option>
    		<option value="H">H</option>
    		<option value="I">I</option>
    		<option value="J">J</option>
    		<option value="K">K</option>
    		<option value="L">L</option>
    		<option value="M">M</option>
    		<option value="N">N</option>
    		<option value="O">O</option>
    		<option value="P">P</option>
    		<option value="Q">Q</option>
    		<option value="R">R</option>
    		<option value="S">S</option>
    		<option value="T">T</option>
    		<option value="U">U</option>
    		<option value="V">V</option>
    		<option value="W">W</option>
    		<option value="X">X</option>
    		<option value="Y">Y</option>
    		<option value="Z">Z</option>
    	</select>
    	<input type="submit" value="Filter">
    </form>

    Write code to generate terms dynamically based on form selection:

    <?php
    $alphabet = $_POST['alphabet'];
    if (isset($alphabet)) {
    $terms = get_terms("directory_cat");
    $count = count($terms);
    $letter = $alphabet;
    	if ( $count > 0 ){
    	$ul = "<ul>";
    		foreach ( $terms as $term ) {
    			if ( preg_match("/^$letter/i",$term->name) ) {
    				echo $ul;
    				$ul = '';  // only display once
    				echo '<li><a href="' . get_term_link($term) . '">'. $term->name . '</a></li>';
    			}
    		}
    		if ( $ul == '') {
    		echo "</ul>";
    		}
    	}
    } else {
    $terms = get_terms("directory_cat");
    $count = count($terms);
    $letter = 'A';
    	if ( $count > 0 ){
    	$ul = "<ul>";
    		foreach ( $terms as $term ) {
    			if ( preg_match("/^$letter/i",$term->name) ) {
    				echo $ul;
    				$ul = '';  // only display once
    				echo '<li><a href="' . get_term_link($term) . '">'. $term->name . '</a></li>';
    			}
    		}
    		if ( $ul == '') {
    		echo "</ul>";
    		}
    	}
    }
    ?>

    One template. One piece of code. DONE.

    Thanks for the help, I hope this post makes someone else’s life mildly less tedious :p

Viewing 12 replies - 1 through 12 (of 12 total)
  • The topic ‘Only displaying terms with the same first letter from a custom taxonomy?’ is closed to new replies.