I'm trying to display a taxonomy description based on which taxonomy the page is set to display (set with a variable)
As it is, I'm only seeing the first description, regardless of which category variable is set.
Here's how I've set it up:
I've built a custom post type called 'product' and set up a taxonomy called'product_type'.
//Register product post type
add_action('init', 'product_register');
function product_register() {
$labels = array(
'name' => ('Products'),
'singular_name' => ('Product'),
'add_new' => ('Add New'),
'add_new_item' => ('Add New Product'),
'edit_item' => ('Edit Product'),
'new_item' => ('New Product'),
'view_item' => ('View Product'),
'search_items' => ('Search'),
'not_found' => ('Nothing found'),
'not_found_in_trash' => ('Nothing found in Trash'),
'parent_item_colon' => ''
);
$args = array(
'labels' => $labels,
'menu_icon' => 'dashicons-tag',
'public' => true,
'has_archive' => true,
'supports' => array('title', 'revisions', 'editor','thumbnail'),
'capability_type' => 'post',
'rewrite' => array("slug" => "product"), // Permalinks format
);
register_taxonomy('product_type', array('product'), array(
'hierarchical' => true,
'label' => 'Product Type',
'singular_label' => 'Type',
'show_ui' => true,
'show_admin_column' => true,
'query_var' => true,
'rewrite' => true)
);
register_post_type( 'product' , $args );
}
In the wordpress back end, I've loaded this with a few terms and have also added in descriptions.
archive-products.php works like a landing page, but will accept information from a variable called $type to filter the content.
if (isset($_GET['type']) || !empty($_GET['type']) ) {
$filtered = true;
$type = $_GET['type'];
}
With that information, I can set up a WP_Query for items that contain that taxonomy term.
if ($filtered == true) {
$type = explode(' ', $_GET['type']);
$taxonomy = array(
array(
'taxonomy' => 'product_type',
'field' => 'slug',
'terms' => $type
)
);
$args = array(
'post_type' => 'product',
'posts_per_page' => -1,
'order' => 'DEC',
'orderby' => 'title',
'offset' => '0',
'tax_query' => $taxonomy
);
$the_query = new WP_Query($args);
if($the_query->have_posts()):
$terms = get_the_terms( $post->ID, 'product_type' );
if($terms) {
foreach( $terms as $term ) {
echo $term->description; //always shows first term description, but should display description based on $type
}
}
while($the_query->have_posts()):$the_query->the_post();
the_title();
the_content();
endwhile;
else: //nothing to show here
endif;} else {
// non-filtered page design
}
The query works great but, as I mentioned earlier, it's only pulling the first taxonomy description.
This is the part that is meant to handle that:
$terms = get_the_terms( $post->ID, 'product_type' );
if($terms) {
foreach( $terms as $term ) {
echo $term->description;
}
}
I had guessed that it needed to be inside the query to land on the correct description, but it doesn't seem to make a difference.
I have also tried this:
$term = get_term_by( $post->ID, $type, 'product_type' );
echo $term->description;
but that doesn't return anything.
I'm out of ideas. I haven't found anything via google or by searching this site.
Any advice is appreciated, thanks in advance.
$_GET['type']
?type=twine%20netwrap%20other
$type = explode(' ', $_GET['type']);
After a few more steps, it looks like this:'terms' => array('twine','netwrap','other')
Just an added level of functionality, but for the project it won't ever pass more than one unless it's done manually.