I have created a custom post type for a product with two custom taxonomies, “colour” and “style”. Products are tagged in both of these. When you go to the taxonomy term page it works as it should i.e. if you go to /colour/blue
it lists all blue products.
What I want to happen is when you go to /colour/blue
is lists all blue products BUT groups them by the second taxonomy “style” showing the first three products with a read more link.
So
Blue > Product Style 1
- Product 1
- Product 2
- Product 3
See More..
Blue > Product Style 2
- Product 3
- Product 4
- Product 5
See More..
Does anyone know if this is possible?
Current code is this, I’ve created a taxonomy template for the term taxonomy-colour.php
<?php get_header(); ?>
<main role="main" class="blinds">
<h1 class="main-product-title">Products</h1>
<!-- section -->
<section class="main-product-content">
<h2><?php $term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) ); echo $term->name; ?></h2>
<?php if (have_posts()): while (have_posts()) : the_post(); ?>
<!-- article -->
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<!-- post image -->
<?php if ( has_post_thumbnail()) : // Check if thumbnail exists ?>
<?php the_post_thumbnail(array(720,400)); // Declare pixel size you need inside the array ?>
<?php endif; ?>
<!-- /post thumbnail -->
<!-- post title -->
<h4><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></h4>
<!-- /post title -->
<p><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">View ></a></p>
</article>
<!-- /article -->
<?php endwhile; ?>
<?php else: ?>
<!-- article -->
<article>
<h2><?php _e( 'Sorry, nothing to display.', 'html5blank' ); ?></h2>
</article>
<!-- /article -->
<?php endif; ?>
<?php get_template_part('pagination'); ?>
</section>
<!-- /section -->
<div class="sidebar-wrapper">
<?php if(!function_exists('dynamic_sidebar') || !dynamic_sidebar('product-sidebar')) ?>
</div>
</main>
As stated in comments, you need to use
usort()
on thethe_posts
filter to sort your posts according to needs.You can try the following: (NOTE: The following code is untested and requires PHP 5.4+ due to array dereferencing)
The first thing to do is to get the available style terms for your current taxonomy term. For that you can get all the posts IDs of your current taxonomy then get the style terms from them using
wp_get_object_terms
. Then you loop through them, query three posts that have both of those taxonomy terms (colour and style).Here is an example that you can use for your case:
You’ll have to build your read more link by sending the style term and the colour term to your style taxonomy template.