Skip Navigation

[Closed] Nested views

This support ticket is created 4 years, 7 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)

Tagged: 

This topic contains 3 replies, has 2 voices.

Last updated by Christian Cox 4 years, 7 months ago.

Assisted by: Christian Cox.

Author
Posts
#1906799
categories level 2.jpg
categories level 1.jpg

Hello!
I hope you can help me achieve what I'm going to describe (actually, this is a follow up of a previous discussion). I'm building a directory website. I need to list a custom post called "Impianti" and this custom post has a custom taxonomy with 2 nested levels of category. Let me better explain.

This is the structure I need to have in the end:

MAIN CATEGORIES LIST VIEW -> CLICK -> SUB-CATEGORIES VIEW -> CLICK -> MACHINE VIEW

All the previous views have to be distinct kinds of views with different layouts and styles. Is this possible?

I've set up the first level of categories but I don't understand how to create another view for the sub-categories level.
I've tried to follow the documentation and search for a solution but I'm missing something and I don't understand what I have to do.

You can see the website I'm developing at this URL hidden link password: "seeeverything". The section is reachable by clicking on "Impianti" on the main menu.

Can I also share with you the website login so you can understand better what's going on?

I've shared a preview of the structure (see attachments):
categories level 1: MAIN CATEGORIES LIST VIEW
categories level 2: SUB CATEGORIES LIST VIEW (every code in this view is a link to the MACHINE VIEW)

Thank you so much for any help!
Roberto

#1907021

Hi, I can activate private reply fields here so you can share a login securely. I'll take a look at how you have things set up and give you some feedback.

#1907817

Hi,
Do you have any advice or indication about my question? Thank you!

#1908163

Hi, sorry I am typically not available on Saturdays so I did not have a chance to reply yesterday. I'm looking at the site now, and I can see you have a nested View structure set up on this page:
hidden link
The parent View is "Impianti - Parent":
hidden link

The parent View loops over taxonomy terms in the taxonomy Categorie Impianti, where the term parent is None - in other words, top-level terms. In each result, you have displayed a nested View, Impianti - livello 1:
hidden link
This View loops over the terms whose parent is set by the parent taxonomy View. Each result in the livello 1 View is displayed with a link to the taxonomy term archive in the results.

I've set up the first level of categories but I don't understand how to create another view for the sub-categories level.
I can see that your livello 1 View is set up to include links to the taxonomy archive URL for each result. To design the taxonomy archive page, you can use Toolset's WordPress Archive feature. This allows you to design an archive for displaying information at the term archive pages like:
hidden link
hidden link
.. and so on for other terms. This archive can include a list of posts that are associated with the archive term, and it can also include a View of taxonomy terms like your livello 1 View. You can configure the taxonomy term filter so that it displays terms where the parent is the same as the current taxonomy archive term. This would allow you to display a list of child terms. I think this would work well for your scenario. You should create a WordPress Archive for the Categorie Impianti taxonomy. I think you should also create a View similar to the livello 1 View, but change the taxonomy term filter so that it displays terms whose parent term is the same as the current archive. Insert this View in your WordPress Archive for the Categorie Impianti taxonomy.

All the previous views have to be distinct kinds of views with different layouts and styles. Is this possible?
Toolset's WordPress Archives are assigned per taxonomy, not per hierarchical level. In other words, by default the same WordPress Archive is displayed for top level terms, sub-level terms, sub-level child terms, and so on. If you need to display a different WordPress Archive for different terms in the same taxonomy, you must use some custom code with our API filter wpv_filter_force_wordpress_archive:
https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_force_wordpress_archive

I have some custom code snippets that might be useful here. For example, let's say you want to display a different WordPress Archive depending on the hierarchical level of a taxonomy term. Consider you have a custom taxonomy with the following terms and hierarchy:
Term 1
- Sublevel term 1
-- Sublevel child term 1
Term 2
- Sublevel term 2
-- Sublevel child term 2

Let's assume you have created 3 WordPress Archives, and you want to display different archives per level of the taxonomy, for example:
Term 1 - Display WP Archive A
- Sublevel term 1 - Display WP Archive B
-- Sublevel child term 1 Display WP Archive C
Term 2 - Display WP Archive A
- Sublevel term 2 - Display WP Archive B
-- Sublevel child term 2 Display WP Archive C

It sounds like this is similar to what you want to achieve, with a different design for the page based on the taxonomy hierarchy level. Here is the custom code snippet that can help you implement something like this:

/* ----------------------------------------------------------------------- */
// DIFFERENT WORDPRESS ARCHIVES PER LEVEL OF TAXONOMY TERM HIERARCHY
// https://toolset.com/forums/topic/nested-views-3/

function get_tax_level($id, $tax){
    $ancestors = get_ancestors($id, $tax);
    return count($ancestors)+1;
}

add_filter( 'wpv_filter_force_wordpress_archive', 'switch_tax_archive_by_level', 30, 2 );
function switch_tax_archive_by_level( $wpa_assigned, $wpa_loop ) {
  $tax_slug = 'categoria-impianti';
  $level_one_archive = 123;
  $level_two_archive = 234;
  $level_three_archive = 345;
  $wpa_to_apply = $wpa_assigned;
  $current_taxonomy = isset(get_queried_object()->taxonomy) ? get_queried_object()->taxonomy : null;
  // only for one specific taxonomy
  if( !$current_taxonomy || $current_taxonomy != $tax_slug )
    return $wpa_to_apply;

  $current_term_level = get_tax_level(get_queried_object()->term_id, $current_taxonomy);
  if ($current_term_level == 1) {
    // show top-level archive
    $wpa_to_apply = $level_one_archive;
  } else if ($current_term_level == 2) {
    // show mid-level archive
    $wpa_to_apply = $level_two_archive;
  } else {
    // show third-level archive
    $wpa_to_apply = $level_three_archive;
  }
  return $wpa_to_apply;
}

You would change 123 to match the numeric ID of the WordPress Archive you want to display for top-level terms. You would change 234 to match the numeric ID of the WordPress Archive you want to display for the second level terms, and change 345 to match the numeric ID of the WordPrwess Archive you want to display for the 3rd level terms.

I can help you adjust this code as needed if you have more than 3 levels, just let me know.

The topic ‘[Closed] Nested views’ is closed to new replies.