Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Custom Taxonomy Loop WordPress

  • SOLVED

Hi,

I was hoping someone could help me with a custom loop for a custom taxonomy

I have a custom post type called 'wff-shop' and a custom taxonomy called 'shop-category'.
There are 4 x categories in the taxonomy ('Bakery, The Cupboard, Meat, Fruit and Veg') and within each of these categories there are sub categories. For example Bakery has 'Bread, Cakes, Biscuits etc. For the Bakery landing page i would like to list each of the sub categories with their relevant posts underneath them.
I have attached a visual as to how the page will be laid out however it will need to work something like this:

Category/Term Title 1
- Post 1
- Post 2
- Post 3

Category/Term Title 2
- Post 1
- Post 2
- Post 3

Category/Term Title 3
- Post 1
- Post 2
- Post 3

and so on.

Each post will be output in a div with a thumbnail, title and price however i have all of the html markup for this..

Thanks

Answers (2)

2013-11-24

Arnav Joy answers:

try this

<?php
$queried_object = get_queried_object();
$term_id = $queried_object->term_id;

$taxonomy_name = 'shop-category';
$termchildren = get_term_children( $term_id, $taxonomy_name );

if( !empty( $termchildren ) ) {
echo '<ul>';
foreach ( $termchildren as $child ) {
$term = get_term_by( 'id', $child, $taxonomy_name );
echo '<li>'. $term->name ;
$args = array(
'post_type' => 'wff-shop',
'tax_query' => array(
array(
'taxonomy' => $taxonomy_name,
'field' => 'slug',
'terms' => $term->slug
)
)
);
$query = new WP_Query( $args );
if( $query->have_posts() ) {
echo '<div>';
while( $query->have_posts() ) {
$query->the_post();
echo '<h2>'. get_the_title().'<h2>' ;
echo '<span>'.get_the_post_thumbnail().'<span>';
}
echo '</div>';
}
echo '</li>';
}
echo '</ul>';
}
?>

2013-11-24

Maryke Janse van Rensburg answers:

I make use of the following

<?php

/**

*

* archive-flipbooks.php

*

* The archive template. Used when flipbooks is queried.

* Note that this template will be overridden by category.php, author.php, and date.php for their respective query types.

*

* More detailed information about template’s hierarchy: http://codex.wordpress.org/Template_Hierarchy

*

*/

get_header(); ?>

<?php get_sidebar('top'); ?>

<div id="contents">
<?php
if ( get_query_var('paged') ) $paged = get_query_var('paged');
if ( get_query_var('page') ) $paged = get_query_var('page');

$query = new WP_Query( array( 'post_type' => 'flipbooks', 'paged' => $paged ) );

if ( $query->have_posts() ) : ?>

<?php while ( $query->have_posts() ) : $query->the_post(); ?>

<div class="art-layout-cell layout-item-0">
<div class="multiple-borders">
<div class="art-postmetadataheader">
<center><h1 class="art-postheader"><a style="text-decoration:none" href="<?php the_permalink() ?>" rel="bookmark" title="View <?php the_title(); ?>"><font color="#000000"><?php the_title(); ?></font></a></h1></center>
</div>
<div class="infobox">
<center><?php the_post_thumbnail( array(180,'270') );?></center>
<div class="more">
<div class="polaroid">
<p><?php echo get_post_meta($post->ID, 'short_description', true); ?></p>
<h3><a style="text-decoration:none" href="<?php the_permalink() ?>" rel="bookmark" title="View <?php the_title(); ?>"><font color="#000000">View Magazine</font></a></h3></div></div>
</div>
</div>
</div>
<?php endwhile; wp_reset_postdata(); ?>
<!-- show pagination here -->
<?php else : ?>
<!-- show 404 error here -->
<?php endif; ?>
</div>

<?php get_sidebar('bottom'); ?>

<?php get_footer(); ?>


The result (with some css tweaking) can be seen here:

http://mailnetwork.co.za/test2/flipbooks/