Skip Navigation

[Resolved] Custom taxonomy output

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

Problem: I would like to apply different WordPress Archives to each level of hierarchy in a custom taxonomy.

Solution: Use the wpv_filter_force_wordpress_archive filter to programmatically specify different WordPress Archives based on the taxonomy term hierarchy level:

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;
  $tax = get_queried_object() ? get_queried_object()->taxonomy : null;
  if ($tax == 'viagens') {
    $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 = 1761;
    } else if ($current_term_level == 2) {
      // show mid-level archive
      $wpa_to_apply = 1722;
    } else {
      // show third-level archive
      $wpa_to_apply = 1722;
    }
  } elseif ( is_home() ) {
    $wpa_to_apply = 1771;
  }
  return $wpa_to_apply;
}

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

This support ticket is created 7 years, 2 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 22 replies, has 2 voices.

Last updated by JorgeE7101 7 years, 2 months ago.

Assisted by: Christian Cox.

Author
Posts
#574490

Hi Christian. That is fine. Would you like to stop all editing in the site for now or is it fine if I continue adding content to other areas of the site?

#574492

You can continue editing, as long as the pages we've been discussing are still present I'll have what I need.

#574500

Thanks. It's all there, including the functions code on the child-theme.

#574550

Okay I have made the following change on your site:

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;
  $tax = get_queried_object() ? get_queried_object()->taxonomy : null;
  if ($tax == 'viagens') {
    $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 = 1761;
    } else if ($current_term_level == 2) {
      // show mid-level archive
      $wpa_to_apply = 1722;
    } else {
      // show third-level archive
      $wpa_to_apply = 1722;
    }
  } elseif ( is_home() ) {
    $wpa_to_apply = 1771;
  }
  return $wpa_to_apply;
}

Now I see:
- Homepage: 2 columns + sidebar, with Viagens terms appearing
- Category page (/ofertas-viagem/): title displayed once, Viagens terms appearing
- Continent page (/viagens/america-do-norte/): title displayed once, Viagens terms appearing
- Country page (/viagens/america-do-norte/estados-unidos/): title displayed once, Viagens terms appearing

Can you confirm? Am I missing anything?

#574551

Hi Christian.

It all looks fine to me.

Thanks very much for everything!

And apologies for so many questions.

Thanks again.

#574562

Sorry to bother you again.

I was now trying to add content to the output editor and I've tried to add these shortcodes:

[wpv-post-taxonomy type="viagens"]
[wpv-post-taxonomy type="viagens" format="url"]
[wpv-post-taxonomy type="viagens" format="name"]

And none of them shows up on the page.

Do you have idea why? All seems to be working except the taxonomy.

This one, however, works:

[wpv-taxonomy-archive info="name"]

It doesn't work in the output editor, but it does work if I put in the template. Am I doing something wrong?

Thanks.

#574655
[wpv-post-taxonomy type="viagens"]
[wpv-taxonomy-archive info="name"]

These shortcodes are meant to do different things. "wpv-post-taxonomy" is meant to be used inside a post Loop or post Content Template to display information about the current post in the loop. "wpv-taxonomy-archive" is meant to be used in a taxonomy Archive to display information about the current archive.

More info about these shortcodes here:
https://toolset.com/documentation/user-guides/views-shortcodes/#wpv-taxonomy-archive
https://toolset.com/documentation/user-guides/views-shortcodes/#wpv-post-taxonomy

#574813

Ok. Thanks for explaining that Christian.