Skip Navigation

[Resolved] Separating out category and sub category content

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.

This topic contains 22 replies, has 3 voices.

Last updated by emilyT 1 year, 9 months ago.

Assigned support staff: Shane.

Author
Posts
#1343971

Hi folks

Could you help me work out how best to display category and sub category content please? I would like to separate out the content as follows:

1. Top level category
List of items assigned to just the top level category

1.1 Sub category
List of items within the first sub category

1.2 Sub category
List of items within the second sub category

You can see my progress here: hidden link
The first two items in the list are top level, then the second two are each in a sub category. Can I separate them out into separate lists?

Cheers, Emily

#1344197

Christian Cox
Supporter

Languages: English (English )

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

Hi, I take it in this case "advertisements" is a top-level category. I also assume that you want to include this display on all the term archive pages for this research-category taxonomy, not just the advertisements term archive. What should happen if someone visits a subcategory archive? Is the top level still "advertisements", or is the top level now the subcategory? Will there ever be more than one level of subcategory, like a sub-sub-category or sub-sub-sub-category?

#1344251

Hi Christian

Yes, that's right "advertisements" is the top-level. I've created a WordPress Archive for the top level which displays the contents of that category.

Each top level category will have the same display.

That's a good question about how the subcategories will work. Initially I'm going on the basis that we will display the contents on the top level. But it could be that we have a list of the subcategories which you click on to be taken to a page with the same layout as the top level. Is either of the two options easier to achieve? Quite happy to go for the line of least resistance.

I'm going to limit it to one level of subcategory. I think it will be too complicated to add any deeper levels.

Many thanks for your help!
Cheers, Emily

#1344419

Christian Cox
Supporter

Languages: English (English )

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

Wordpress will create an archive URL for all the terms in a taxonomy, regardless of the hierarchy. So if someone types the URL in manually, they will be shown an archive even if you don't create links to that archive. Toolset's WordPress Archives will be applied to all hierarchical levels of a taxonomy, by default, unless you add some custom code. So I guess the easiest approach is to create one WordPress Archive and let that be applied to all hierarchical levels.

In that WordPress Archive, you will include a View of terms, filtered by term ID, set by a shortcode attribute. Pass in the current archive taxonomy term ID using the wpv-taxonomy-archive shortcode:

[wpv-view name="your-view-slug" terms="[wpv-taxonomy-archive info='id']"]

Insert the term name or something similar in the loop, to produce the top-level term list. Inside that View, just after the term name, you will nest another View of terms, filtered by term parent, set by the current loop. This will produce the child terms list, so insert the term name or something similar in the loop.

Inside each of these term Views, you'll insert a View of posts filtered by term. Let's see if we can get that far, then I can share some custom code that will help filter out duplicates.

#1344461

Thanks Christian. I'm not entirely sure I follow your explanation, but I'll try to break it down and see how far I get.

I think from what you've said that my first step should be to create a view. I'll have a look at that.

Cheers, Emily

#1344561

Christian Cox
Supporter

Languages: English (English )

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

Okay sure, I can jump in if you get stuck.

#1345503

Thanks Christian. I'm currently away for a few days, so I will come back to you on this as soon as I've had a chance to look at it.

#1346375

Hi Christian

I've got a bit further with this. I've created a view which lists the sub categories of the parent category for the page you are on, which you can see on the advertisements page: hidden link

What I'd like to do now is adjust the list of items for the partent category so they don't display sub cat items. In this example it's currently showing:

Advertising in Lyme Regis 1930
Somewhere to stay in Lyme Regis
Sub Category Download 1
Sub Category Download 2

The top two are directly in the advertisements category so I would like those to display, but the bottom two are in sub categories. Can you help me filter them out please?

Many thanks, Emily

#1346461

Christian Cox
Supporter

Languages: English (English )

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

Okay I have a custom code snippet from another ticket that may be useful in this project:

add_filter( 'wpv_filter_query', 'not_all_my_children_term_filter',99,3 );
function not_all_my_children_term_filter( $query_args, $views_settings, $view_id) {
  $view_ids = array( 70 );
  if (in_array($view_id, $view_ids)){
    foreach($query_args['tax_query'] as $tq) {
      if( isset( $tq['taxonomy'] ) ){
        $tax = $tq['taxonomy'];
        $term = get_term_by('id', $tq['terms'], $tax);
        $termChildren = isset( $term->term_id ) ? get_term_children($term->term_id, $tax) : null;
        $query_args['tax_query'][] = array(
          'taxonomy' => $tax,
          'field' => 'id',
          'terms' => $termChildren,
          'operator' => 'NOT IN'
        );
      }
    }
  }
  return $query_args;
}

You can see I have limited this filter to run only on View #70. I believe this is the top-level View, but please correct that if I'm wrong. Then add this code to your child theme's functions.php file, or to a new code snippet in Toolset > Settings > Custom Code.

#1348163

Hi Christian, sorry for not replying sooner, I was travelling all day yesterday so haven't had a chance to try it out yet. I'll be back in touch as soon as I'm caught up.

I really appreciate your help with this!
Cheers, Emily

#1348299

Christian Cox
Supporter

Languages: English (English )

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

No problem, I'll stand by for your update.

#1350811

Hi Christian

I've added the code to my functions but it hasn't made any difference: hidden link

Is it because the first list of items is generated in the WP Archive rather than by a view? I'm happy to give you access to the site if it would be easier for you to have a look.

Thanks for your continued help!
Best wishes, Emily

#1352497

Hi Christian

Just a quick follow up. Are you able to offer any further guidance on this?

Many thanks, Emily

#1353211

Shane
Supporter

Languages: English (English )

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

Hi Emily,

As Christian is currently on vacation at the moment, I will be looking after his queue.

I see that christian specified a view id of 70 in the code. Is your view ID 70 ? If not then you will need to change the ID to the ID of your view in order to get it to work.

Also where did you add the snippet? If it is in our Toolset custom code section then you need to click on the activate button in order for the snippet to work.

Please let me know.
Thanks,
Shane

#1353913

Hi Shane

Thanks for picking this up.

So what I've got is this: hidden link
Looking at this list:
Advertising in Lyme Regis 1930
Somewhere to stay in Lyme Regis
Sub Category Download 1
Sub Category Download 2
I want to filter out the bottom two as they belong to the sub categories.

The list is generated by the WordPress Archive (which has an id 0f 70).

I've added the snippet to functions.php for the theme - it's a custom theme so I've put it directly into the parent theme functions.

Many thanks, Emily