Skip Navigation

[Resolved] Show term archive links on term archive

This thread is resolved. Here is a description of the problem and solution.

Problem: I would like to display a list of term archive links on hierarchical term archive page. The list should display the parent term and any sibling terms of the current archive term.

Solution:
- Create a View of the Product Category taxonomy. Add a term parent Query Filter, where the term parent is set by the parent taxonomy archive View. Make sure "Don't show empty terms" is unchecked if you want to show all sibling terms regardless of their post count.
- In the Loop Output of this View, insert the wpv-taxonomy link shortcode for now. We will come back to this to display the current term with a highlight.
- Create another View of the Product Category taxonomy. Add a term Query Filter, where the term ID is set by one shortcode attribute "terms".
- In the Loop Output of this View, insert the wpv-taxonomy link shortcode. This will display the parent term. After that, insert the View you created in step 1.
- Add this custom code to functions.php:

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_slug = get_term_parents_list( $term, $tax, array('format'=>'slug', 'link'=>FALSE, 'inclusive'=>FALSE, 'separator'=>''));
    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");

- Create the WordPress Archive for the child-level Product Category taxonomy, if it does not exist yet.
- In the Loop, insert your second View, the one with the Query Filter using a shortcode attribute. Use the custom shortcode to pass in the parent term's ID:

[wpv-view name="product-category-sidebar" terms="[get_term_parent return='id']"]

- Register get_term_parent in Toolset > Settings > Frontend Content > Third party shortcodes

This support ticket is created 6 years, 3 months ago. There's a good chance that you are reading advice that it now obsolete.

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.

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 2 replies, has 2 voices.

Last updated by Christian Cox 6 years, 3 months ago.

Assisted by: Christian Cox.

Author
Posts
#953137
952287-2_min.jpg

I want to set up a sidebar in child term archive & sub child term archive pages, where the sidebar should only display the parent, child & siblings of the current archive page term. (see screenshot 2)

please help me with this, it is very important.

#953174

Hi, here's how I would set this up.
- Create a View of the Product Category taxonomy. Add a term parent Query Filter, where the term parent is set by the parent taxonomy archive View. Make sure "Don't show empty terms" is unchecked if you want to show all sibling terms regardless of their post count.
- In the Loop Output of this View, insert the wpv-taxonomy link shortcode for now. We will come back to this to display the current term with a highlight.
- Create another View of the Product Category taxonomy. Add a term Query Filter, where the term ID is set by one shortcode attribute "terms".
- In the Loop Output of this View, insert the wpv-taxonomy link shortcode. This will display the parent term. After that, insert the View you created in step 1.
- Add this custom code to functions.php:

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_slug = get_term_parents_list( $term, $tax, array('format'=>'slug', 'link'=>FALSE, 'inclusive'=>FALSE, 'separator'=>''));
    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");

- Create the WordPress Archive for the child-level Product Category taxonomy, if it does not exist yet.
- In the Loop, insert your second View, the one with the Query Filter using a shortcode attribute. Use the custom shortcode to pass in the parent term's ID:

[wpv-view name="product-category-sidebar" terms="[get_term_parent return='id']"]

- Register get_term_parent in Toolset > Settings > Frontend Content > Third party shortcodes

#953201

Edit - I made a few changes here so we can reuse the same code for both tickets.