Problem: I would like to display a list of related posts on a taxonomy term archive. Related posts should be posts with terms that are siblings of the current archive term.
Solution:
- Create a View of these posts filtered by the same taxonomy, where the taxonomy term slug is set by one shortcode attribute.
- Use a custom shortcode to get the parent term slug based on the current archive term slug.
function get_term_parent_func($atts) { $a = shortcode_atts( array( 'taxonomy' => '', 'return' => 'id' ), $atts ); $taxonomy = $a['taxonomy']; $parent_id = 0; if( is_tax( $taxonomy ) ) { $taxObj = get_queried_object(); $tax = isset($taxObj->taxonomy) ? $taxObj->taxonomy : 0; $term = isset($taxObj->term_id) ? $taxObj->term_id : 0; $parent_slugs = get_term_parents_list( $term, $tax, array('format'=>'slug', 'link'=>FALSE, 'inclusive'=>FALSE, 'separator'=>',')); $parent_slugs = explode(',', $parent_slugs); $depth = sizeof($parent_slugs) - 1; $parent_slug = ($depth === 0) ? $taxObj->slug : $parent_slugs[$depth-1]; if( $a['return'] == 'id' ) { $parent = get_term_by( 'slug', $parent_slug, $tax ); $parent_id = isset($parent->term_id) ? $parent->term_id : $taxObj->term_id; return $parent_id; } if( $a['return'] == 'slug' ) { return $parent_slug; } } return $parent_id; } add_shortcode("get_term_parent", "get_term_parent_func");
- Pass that value into your View of posts as a shortcode attribute:
[wpv-view name="products-in-parent-term" wpvproductcat="[get_term_parent taxonomy='product_cat' return='slug']"]
- Register the get_term_parent shortcode in Toolset > Settings > Third party shortcode arguments
- Apply a custom filter using PHP that will filter out all the posts in the current term archive, to prevent duplicates.
add_filter( 'wpv_filter_query', 'not_this_term_filter',99,3 ); function not_this_term_filter( $query_args, $views_settings, $view_id) { $view_ids = array( 1234 ); $tax = 'product-categories'; $taxObj = get_queried_object(); $tax = isset($taxObj->taxonomy) ? $taxObj->taxonomy : 0; $term_id = isset($taxObj->term_id) ? $taxObj->term_id : 0; if (in_array($view_id, $view_ids)){ $query_args['tax_query'][] = array( 'taxonomy' => $tax, 'field' => 'id', 'terms' => $term_id, 'operator' => 'NOT IN' ); } return $query_args; }
Relevant Documentation:
https://toolset.com/documentation/user-guides/passing-arguments-to-views/
https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query
This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.
Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.
This topic is split from https://toolset.com/forums/topic/how-to-create-different-taxonomy-archive-page-for-parent-child-taxonomy-terms/
Sun | Mon | Tue | Wed | Thu | Fri | Sat |
---|---|---|---|---|---|---|
8:00 – 12:00 | 8:00 – 12:00 | 8:00 – 12:00 | 8:00 – 12:00 | 8:00 – 12:00 | - | - |
13:00 – 17:00 | 13:00 – 17:00 | 13:00 – 17:00 | 13:00 – 17:00 | 13:00 – 17:00 | - | - |
Supporter timezone: America/New_York (GMT-04:00)
This topic contains 8 replies, has 2 voices.
Last updated by 6 years, 5 months ago.
Assisted by: Christian Cox.