• Hello,

    I’m completely stuck with this, also my knowledge about WordPress and PHP is virtually null, so I would gladly accept any help in this matter.

    I have registered a custom post type “Animals” and also two custom taxonomies (“Vertebrate” and “Type”) assigned to it, which have their own specific terms, for “Vertebrate” it will be “Mammals” and “Reptiles”, and for “Type” it will be “water type” and “ground type”.

    By using page.php I would like to display a list of all custom posts (Animals) that are sorted by terms that are assigned to the “Vertebrate” custom taxonomy, in this I succeed by using the below code:

    <?php
            $post_type = 'animals';
            $tax = 'vertebrate';
            $tax_terms = get_terms($tax);
    
            if ($tax_terms) {
              foreach ($tax_terms  as $tax_term) {
                $args=array(
                  'post_type'       => $post_type,
                  "$tax"            => $tax_term->slug,
                  'post_status'     => 'publish',
                  'posts_per_page'  => -1,
                  'caller_get_posts'=> 1,
                  'orderby'         => 'title',
                  'order'           => 'ASC'
                );
    
                $my_query = null;
                $my_query = new WP_Query($args);
    
                if( $my_query->have_posts() ) {
    
                  echo '<h2>'. $tax_term->name .'</h2>';
    
                  while ($my_query->have_posts()) : $my_query->the_post(); ?>
    
                    <p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p>
    
    <?php
    
                  endwhile;
                }
                wp_reset_query();
              }
            }
    ?>

    This gives me something like this:

    Mammals:

    • Cow
    • Dog
    • Dolphin
    • Orca
    • Whale

    Reptiles

    • Lizard
    • Sea Turtle

    However I would also like to display in the foregoing list to which terms of the “Type” taxonomy each post is assigned to (besides terms from the Vertebrate taxonomy) by simply returning their name after the post link.

    I’m trying to achieve something like this:

    Mammals:

    • Cow (ground type)
    • Dog (ground type)
    • Dolphin (water type)
    • Orca (water type)
    • Whale (water type)

    Reptiles

    • Lizard (ground type)
    • Sea Turtle (water type)
Viewing 1 replies (of 1 total)
  • Try changing this:

    while ($my_query->have_posts()) : $my_query->the_post(); ?>
    
                    <p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p>

    to this (untested):

    while ($my_query->have_posts()) : $my_query->the_post(); ?>
    
    <?php $type_list = '';
    $type_terms = get_the_terms( $post->ID, 'type' );
    if ( $type_terms && ! is_wp_error( $type_terms ) ) {
    	$type_names = array();
    
    	foreach ( $type_terms as $one_type ) {
    		$type_names[] = $one_type->name;
    	}
    
    	$type_list = ' (' . join( ", ", $type_names ) . ')';
    }
    ?>
    <p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); echo $type_list; ?></a></p>
Viewing 1 replies (of 1 total)
  • The topic ‘Adding a term name from a custom taxonomy assigned to a post from a query?’ is closed to new replies.