Skip Navigation

[Resolved] Output different templates with one taxonomy

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

Problem: I would like to use different WordPress Archive templates to display archives for terms in a single taxonomy, based on the term's hierarchy.

Solution:
Create 3 different WordPress Archives, one for each hierarchical level in your taxonomy. Use the wpv_filter_force_wordpress_archive filter to conditionally apply each template based on the taxonomy term's hierarchy. Example:

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 ) {
  $wpa_to_apply = $wpa_assigned;
  $current_term_level = get_tax_level(get_queried_object()->term_id, get_queried_object()->taxonomy);
  if ($current_term_level == 1) {
    // show top-level archive
    $wpa_to_apply = 123;
  } else if ($current_term_level == 2) {
    // show mid-level archive
    $wpa_to_apply = 456;
  } else {
    // show third-level archive
    $wpa_to_apply = 789;
  }
  return $wpa_to_apply;
}

Replace 123, 456, 789 with the IDs of each WordPress Archive and place the code in functions.php

Relevant Documentation: https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_force_wordpress_archive

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.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

Author
Posts
#573320

Hello. My goal is to output slight different informartion according to the taxonomy applied.

So far I have created a hierarchical taxonomy named "Trips". The taxonomy is set like this: Continent - Country - City

This taxonomy has term fields which I want to output in a WordPress archive template to display with the country pages only. I don't want to use this template with Continents, or Cities. I want to use a slight different template, because most of the term fields (population, airports, etc) do not apply to Continents or Cities or any other taxonomy other than countries.

To be clear of what I'm thinking to achieve, I have two different scenarios:

Scenario 1:

The user clicks on the Taxonomy term "Russia" and it displays the archive template for the country with all the term fields.

Scenario 2:

The user clicks on the Taxonomy term "Europe" and it displays the archive template for the continent with just the posts loop and a short intro.

How can I achieve this using just one taxonomy? Is possible to use conditionals or filter or some settings that allow me to use a template just for certain taxonomy terms?

This site uses your plugin and has, somehow, achieved that:

Continent taxonomy page:

- link removed -

Country taxonomy page:

- link removed -

City taxonomy page:

- link removed -

Thanks.

#573455

Christian Cox
Supporter

Languages: English (English )

Timezone: America/New_York (GMT-04:00)

The best approach is to use PHP to modify the WordPress Archive used for a page using our filter wpv_filter_force_wordpress_archive. This would allow you to create separate WordPress Archives for each design, and eliminate the need for conditionals in your WordPress Archive editor. Our filter is described here:
https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_force_wordpress_archive

Example:

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 ) {
  $wpa_to_apply = $wpa_assigned;
  $current_term_level = get_tax_level(get_queried_object()->term_id, get_queried_object()->taxonomy);
  if ($current_term_level == 1) {
    // show top-level archive
    $wpa_to_apply = 123;
  } else if ($current_term_level == 2) {
    // show mid-level archive
    $wpa_to_apply = 456;
  } else {
    // show third-level archive
    $wpa_to_apply = 789;
  }
  return $wpa_to_apply;
}

Modify the 123, 456, and 789 to match the IDs of each WordPress Archive and you're good to go.

#573676

Hi thanks. That seems to be the solution.

I've now created 2 different wordpress archives. One of Countries and one for continents.

And I've tried to add the code to archive.php, but the code just shows up on the live page. And I can't assign the archive to the continent archive as it's already assigned to Countries continent.

What am I doing wrongly?

Thanks.

#573851

Christian Cox
Supporter

Languages: English (English )

Timezone: America/New_York (GMT-04:00)

When you add custom code to a theme, typically you should add it in functions.php. Try it there instead.

#573858

Brilliant stuff! It's working now! Thanks a lot Christian.

#1724529

Hello. Would it be possible to remove all links and images from this thread, please?

Thank you.

#1725097

Christian Cox
Supporter

Languages: English (English )

Timezone: America/New_York (GMT-04:00)

All external links and images have been removed.