• I’m trying to get a list of terms for a custom loop.
    I’ve tried some forms to get that, but can’t figure out what is going whrong.

    heres the thing:
    there’s a custom post called photos, that has 2 taxonomies: photo_year and photo_tags.

    there are two pages, each one displaying photos from each year.

    i can get the list of therms for the photo_tags taxonomy with this piece of code:

    $terms = get_terms("photo_tags");
    	$count = count($terms);
    	if ( $count > 0 ){
    	     foreach ( $terms as $term ) {
    	     echo '
    <li><a>slug .'" class="foto-filter-button '. $term->slug .'">'.$term->name.'</a></li>
    ';
    	     }
    	 }

    but this returns a list of all terms, including those applied to the posts of the 2012 year.

    so, I run out a loop:

    <?php
    query_posts( array (
    'post_type' => 'photo', 'photo_year' => '2011', 'orderby' => 'title', 'order' => 'ASC', 'posts_per_page' => -1) );

    and then a code for getting the terms for these posts:

    $terms = wp_get_post_terms($post->ID,'photo_tags');
    	foreach($terms as $term){
    		echo '
    <li><a>slug .'" class="foto-filter-button '. $term->slug .'">'.$term->name.'</a></li>
    ';
    	}

    this code get the terms, but it gets all of them, repeating a lot (because there are single tags applied to multiple posts).
    so, I would like to know how to get the single terms, without repeating each entry every time it apears in a post in the loop.

    I think that it must be something looking for repeated id’s of the terms or something like that.

    ps.: sorry for the bad english.

Viewing 4 replies - 1 through 4 (of 4 total)
  • Moderator bcworkz

    (@bcworkz)

    You could always directly query the taxonomies table, along the lines of $wpdb->get_results("SELECT DISTINCT...");
    Sorry, I don’t know exactly how to get what you need out of the table, but what I’ve shown should get you pointed in the right direction.

    Thread Starter hugoassuncao

    (@hugoassuncao)

    thank you bcworkz, I’ll give a shot.

    Thread Starter hugoassuncao

    (@hugoassuncao)

    I found a way to get all the terms that I want.

    query_posts( array ('post_type' => 'photo', 'photo_ano' => '2011', 'orderby' => 'title', 'order' => 'ASC', 'posts_per_page' => -1) );
    	while ( have_posts() ) : the_post();
    
    		$postterms = get_the_terms( $post->ID, 'photo_tags' );
    			if ($postterms) {
    				foreach($postterms as $term) {
    					$all_terms[] = $term->name;
    				}
    			}
    		endwhile;
    
    			$terms = array_unique($all_terms);
    				foreach ($terms as $term) {
    					echo '<li><a href="#'.$term->slug.'" class="foto-filter-button '.$term->slug.'">'.$term.'</a></li>';
    				}

    but, this solution gives me a one dimensional array, because of the $all_terms[] = $term->name; piece of code.
    so, I can’t get the slug, just the name of the term.

    is that a way to put two values (name and slug) in that array so I can print out these values separated?

    Moderator bcworkz

    (@bcworkz)

    I’m weak on php array manipulation, so I may be wrong, but can’t you do something like:

    $all_terms[]['name'] = $term->name;
    $all_terms[]['slug'] = $term->slug;

    Just an idea, sorry I don’t have a definitive answer.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘loop through custom terms’ is closed to new replies.