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)
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.
UPDATE
My friend helped me out with this. It should work with this:
<?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>
<?php
setup_postdata( $post );
$terms = wp_get_post_terms( $post->ID, 'type');
echo '<span>'. ($terms[0]->name) .'</span></p>'; ?>
<?php
endwhile;
}
wp_reset_query();
}
}
?>