Skip Navigation

[Resolved] Change Custom Taxonomy slug to force desired URL structure, update affected code

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

Last updated by liatG 6 years, 1 month ago.

Assisted by: Christian Cox.

Author
Posts
#1120102

Hi Christian,
I am in my last step of putting together all my online classes. In terms of the 4 kinds of navigation that I wanted on my class pages (URL, breadcrumb, sidebar, and per-page), the last two work exactly how I want.

I would like help making a base change to the class-part taxonomy slug (I think) in order to make the following happen:

URL structure:
Desired result:
My Classes page at knitfreedom.com/my-classes
Individual classes (class-part taxonomy archives) at knitfreedom.com/my-classes/class-name

Right now the structure looks like:
My Classes page at hidden link
Individual classes at hidden link

Side note: I did try a URL manipulation plugin but it basically just makes redirects to the old URL. Plus you have to make the change on each page, and I have 570 Online Class Chapters affected by this setup. Anyway-

I made a backup and started experimenting. If I change the class-part taxonomy slug to "my-classes" and delete the first-level term (My Classes) from the taxonomy, the URLs of the individual classes work as desired. However, there is no page at knitfreedom.com/my-classes (just like there is no page right now at knitfreedom.com/class-part).

Breadcrumb structure:
Desired breadcrumbs:
My Classes > Taxonomy Term Level 1 > Taxonomy Term Level 2...
The current breadcrumbs that show up in the classes are: Online Class Chapters > Taxonomy Term Level 1 > Taxonomy Terms Level 2, etc. e.g. currently Online Class Chapters > My Classes > Knitting Superstar.
In Yoast settings I can change the "Online Class Chapter" breadcrumb to say "My Classes." It goes to hidden link, which isn't a useful page for my users. I can redirect that to knitfreedom.com/my-classes, which is what I want, but that page doesn't exist.

Code issues:
Changing the class-part taxonomy slug affects the code snippets you gave me, plus all the layouts based on the taxonomy. I think this is fixable if I document everything beforehand, but I definitely lose the layouts (assigned per taxonomy level) and couldn't get the new my-classes archive pages to show up in the layouts (after I changed all the "class-part" to "my-classes" in the code you gave me).

I am going to request a video call to ask you about this, because I think it merits discussion before trying a bunch of things, and I basically don't see a clear solution to achieve my desired effect. I really appreciate you helping me with this. Because of your help, I've been able to create huge, easily-navigable classes with little effort, and they are exactly what I want (see, for example, hidden link).

Liat

#1120861

Hi Liat, we should be able to adjust the Views, Query Filters, and custom code to work with one less level of hierarchy in the custom taxonomy you're using here. I will be available some next week for a video call, so when my manager receives the request we can try to get something on the schedule.

However, there is no page at knitfreedom.com/my-classes (just like there is no page right now at knitfreedom.com/class-part).
What if you create a custom Page and give it the slug my-classes? Then you can add content to that page, or apply a specific Layout.

#1120942
Custom Code affecting 'class-part' taxonomy.png

Hi Christian,
Thanks so much for the idea! I don't know why I thought it wouldn't let me create a page with slug "my-classes" if I used that as a taxonomy slug as well. I tried it and it works fine.

Since it seems like making the adjustment suggested would work ok, there might not be much to discuss on a call.

If that's the case, would you be available to just type back and forth here to work on adjusting the code? I'm just trying to do whatever is most efficient, so if we don't have to wait until next week, that would be better for me.

To prepare, I moved my custom code from functions.php to the new Custom Code area of Toolset Settings. Everything still works fine. The three pieces of code you gave me are checked in the screenshot.

As I mentioned, I tried just changing each instance of "class-part" to "my-classes," but I couldn't get the page layouts to show anything like what I've been used to, in order to see if the changes had worked. I'd love it if you could walk me through what to change.

Here are the contents of each custom code snippet, in case you can tell me how to modify them.
Filter For N-Level Term

<?php

// Add shortcode to filter for n-level term or ID

function n_level_term_func($atts) {
  $a = shortcode_atts( array(
      'n' => '0',
      'format'=>'id',
  ), $atts );
  $current_taxonomy = get_queried_object()->taxonomy;
  $id = 0;
  $slug = '';
  if( $current_taxonomy == 'class-part' ) {
    $ancestors = get_ancestors( get_queried_object()->term_id, $current_taxonomy );
    $count = count( $ancestors );
    $id = isset( $ancestors[ $count - $a['n'] ] ) ? $ancestors[ $count - $a['n']] : get_queried_object()->term_id;
    $term= get_term( $id, $current_taxonomy);
    $slug = isset($term->slug) ? $term->slug: $slug;
  }
  return $a['format']=='slug' ? $slug : $id;
}
add_shortcode("n-level-term", "n_level_term_func");

Create Archive-Part-Num Shortcode for Page Navigation

<?php

// make dynamic shortcode for next/previous taxonomy navigation

function archive_part_num_func($atts) {
  $current_taxonomy = isset(get_queried_object()->taxonomy) ? get_queried_object()->taxonomy : null;
  if( $current_taxonomy == 'class-part' ) {
    $id = get_queried_object()->term_id;
	$partnum = types_render_termmeta( 'part-order', array('term_id'=>$id));
	return $partnum;
  }
  return;
}
add_shortcode("archive-part-num", "archive_part_num_func");

Custom Layouts for Certain Levels in the Taxonomy

<?php

// Create custom layouts for just certain levels in the taxonomy

function apply_layout_by_archive_term_hierarchy( $id, $layout ){
  // all the taxonomy slugs that should use these hierarchical layouts
  $taxonomies = array( 'class-part' );
  // the layout IDs for each level of hierarchy, separated by a comma
  $layouts = array( 42080, 42081, 42082, 40595 );
 
  // ---------------------------------------------------
  // you should not edit anything below
  // ---------------------------------------------------
 
  $layout_to_apply = $id;
  $current_taxonomy = isset(get_queried_object()->taxonomy) ? get_queried_object()->taxonomy : null;
  // test for the correct taxonomy archive
  if( !$current_taxonomy || !in_array( $current_taxonomy, $taxonomies) )
    return $layout_to_apply;
 
  // find the hierarchical term level and apply the corresponding layout
  $current_term_level = get_tax_level(get_queried_object()->term_id, $current_taxonomy);
  if( isset( $layouts[$current_term_level])){
    return $layouts[$current_term_level];
  }
 
  // fallback to original layout
  return $layout_to_apply;
}
 
add_filter('get_layout_id_for_render', 'apply_layout_by_archive_term_hierarchy', 10, 2);
 
function get_tax_level($id, $tax){
    $ancestors = get_ancestors($id, $tax);
    return count($ancestors);
}

Thank you!
Liat

#1122320

If that's the case, would you be available to just type back and forth here to work on adjusting the code?
Sorry, I didn't get back to this ticket on Thursday and I'm off Friday and Saturday. In our current workflow, each time I provide a reply, the ticket leaves my queue and I move to the next ticket in queue. Client replies then get placed in my queue after any other pending ticket I have assigned. So there's not really a chat back-and-forth workflow at the moment. My schedule is available here: https://toolset.com/forums/users/christian-c/

Filter For N-Level Term
Change line 13 if necessary to use the correct taxonomy slug. My guess is that it should be:

if( $current_taxonomy == 'my-classes' ) {

Note that everywhere you have placed this shortcode, you may need to modify the "n" attribute. This is because everything has been pushed down one level of hierarchy. So you probably need to subtract 1 from the previous "n" value.

Create Archive-Part-Num Shortcode for Page Navigation
Change line 7 if necessary to use the correct taxonomy slug. My guess is that it should be:

  if( $current_taxonomy == 'my-classes' ) {

Otherwise this code should be ok.

Custom Layouts for Certain Levels in the Taxonomy
Change line 7 if necessary to use the correct taxonomy slug. My guess is that it should be:

  $taxonomies = array( 'my-classes' );

I think you also need to change the array of Layout IDs. There is one less level of hierarchy now, so I suspect that the first number in the array should be deleted, resulting in this code:

  $layouts = array( 42081, 42082, 40595 );
#1124868

My issue is resolved now. Thank you!