Skip Navigation

[Resolved] Different archive style for parent and child categories

This support ticket is created 4 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
- 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 -
- 13:00 – 18:00 13:00 – 18:00 13:00 – 18:00 14:00 – 18:00 13:00 – 18:00 -

Supporter timezone: America/Jamaica (GMT-05:00)

This topic contains 10 replies, has 3 voices.

Last updated by deepS 4 years, 2 months ago.

Assisted by: Shane.

Author
Posts
#1799827

Hi,
The site is about online courses listing. The courses are segregated into different child categories which are also a part of the parent category. Example:
1) Parent category-1
- Child category-1
- Child category-2
- Child category-3
2) Parent category-2
- Child category-1
- Child category-2
- Child category-3
and so on.
Now what I want is to create different styles for parent and child categories. Say for example the child categories should simply enlist the online courses under it. Whereas the parent categories should display all the child categories (with ajax filter) on the upper part then in the lower part the list of courses.
Hope I make you understand.
I have gone through some of the previously supported tickets but couldn't make out of them. Please help me to get out of it. Thanks.

#1801357

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

I don't think you can set up an ajax search in the way that you describe, in the context of how WordPress works it sounds like the solution you need is to create a custom archive for this category (which will display the posts matching that category) and insert a taxonomy View at the top of the archive which displays navigation links to the other archives.

Blocks doesn't support taxonomy Views (yet) so you would need to create that using the legacy editor, where the View displayed terms of this taxonomy and included a Query Filter to set the parent term according to the current archive, so that would output the child categories for the current parent. You'd want to output them as links, which you would maybe style like buttons, for example, so using them would take you to a different archive url, and you'd want to output nothing at all if there were no children of the current archive (because you were already on a child archive, for example).

But I suspect you would also want to be able to navigate between parent archives, so you could do something similar, creating another taxonomy View that this time has a Query Filter to say there is no parent term (i.e. only return terms which are parents), and maybe you could output this as a dropdown selector to choose a different parent to move to.

But the key point is that you should be thinking in terms of outputting links to navigate between the archives.

#1801371

I am using Toolset Views.
Ok, leave about ajax filtering (links to respective archives may work)
But how to proceed with? Actually, I haven't created Category archives previously.
Should I create two WordPress archives or Views? Can I have some documentation in this regard? I am a bit confused.
If possible can you create it for me as a sample? I am ready to share the login credentials. Thanks for your support.

#1801949

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Deep,

Based on what you are doing, you can achieve this with a nested view setup.

Have a look at this link below and it should be able to guide you on how to set this up.
https://toolset.com/documentation/user-guides/views/using-a-child-view-in-a-taxonomy-view-layout/#taxonomy-view-within-taxonomy-view

Thanks,
Shane

#1802609

Hi Shane,
Thanks for your support. There are two parts of my query https://toolset.com/forums/topic/different-archive-style-for-parent-and-child-categories/#post-1799827
Part-1: How to design parent and child categories differently?
Part-2: How to show Child categories above the archive list Parent category?

You answered me the Part-2 while Part-1 needs to be resolved first.

For Part-1:
Though I found one previously resolved ticket https://toolset.com/forums/topic/how-to-create-different-taxonomy-archive-page-for-parent-child-taxonomy-terms/ and worked accordingly I couldn't reach the solution.

I have created three archives namely Parent Courses (ID 1759), Child Courses (ID 1760) and Grand Child Courses (ID 1761) to display archives accordingly.
Here is the code I inserted in the child theme function.php file:

// filter that switches wordpress archives for a specific taxonomy based on term hierarchy
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_taxonomy = isset(get_queried_object()->taxonomy) ? get_queried_object()->taxonomy : null;
  // only for course category taxonomy
  if( !$current_taxonomy || $current_taxonomy != 'course_cat' )
    return;
 
  $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 = 1759;
  } else if ($current_term_level == 2) {
    // show mid-level archive
    $wpa_to_apply = 1760;
  } else {
    // show third-level archive
    $wpa_to_apply = 1761;
  }
  return $wpa_to_apply;
}

But it seems to not working.

I have also created one view for parent archive display child categories. Hope you got my points. Please help me to resolve the issue.
Thanks

#1805735

I couldn't understand how long I have to wait? Hope someone will respond.

#1806419

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Deep,

Sorry for the delay in response, our forums are a bit busy at the moment as well as we are short staffed.

However answering your questions.

Part-1: How to design parent and child categories differently?

Right here we have to make a clear distinction in what you are talking. If you are referring to the a view, then we can do this with a view in view setup.

However if it is an Archive we will need a way to check if the term has a parent

With this we can load a different content template for your archive depending on if the term has a parent or not.
In order to do this we will definitely need to use some custom code to check.

So for the parent archive you will display a view listing the child terms, however for the child you will display the list of posts.

A custom shortcode that you can use to check if the term has a parent is the on below.

function wp_get_term_parent( $atts ) {

	// Attributes
	$atts = shortcode_atts(
		array(
			'slug' => '',
			'taxonomy' => '',
		),
		$atts
	);

	$term = get_term_by('slug',$atts['slug'],$atts['taxonomy']);
	if ($term->parent != 0){
	    return true;
	}

}
add_shortcode( 'get_term_parent', 'wp_get_term_parent' );

This shortcode is used like this below.
[get_term_parent slug="[wpv-taxonomy-slug]" taxonomy="category"]

The shortcode will return the number 1 if the term has a parent. With this you can then use our conditional shortcode to check if the custom shortcode returns a 1 or not.
Example

[wpv-conditional if="('[get_term_parent slug='[wpv-taxonomy-slug]' taxonomy='category'] ne '1')"] View that list child terms here[/wpv-conditional]

[wpv-conditional if="('[get_term_parent slug='[wpv-taxonomy-slug]' taxonomy='category'] eq '1')"] View that list Posts of the child term being viewed here[/wpv-conditional]

Now in order to use the shortcode you will first need to add it to the Toolset custom code section in Toolset -> Settings -> Custom code and then activate it.

To use it in the conditional shortcode you will need to add it to the 3rd party shortcode arguments section in Toolset -> Frontend . Add the shortcode name get_term_parent.

Please let me know if this helps.
Thanks,
Shane

#1806777
Screenshot_1.jpg

Hi,
I have used this shortcode in my WordPress archive for Course Category (ID 1766). Please see the screenshot. I have inserted the example code given by you but they are not showing anything. Please check these archive pages:
hidden link
hidden link
Please check if anything left unattended.

#1809169

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Deep,

Can you allow me to have admin access to the site so that I can check to see what is happening here ?

Thanks,
Shane

#1810433

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Deep,

It should now be working.

The issue was with a missing quote in the conditional.

Thanks,
Shane

#1810889

My issue is resolved now. Thank you!