Skip Navigation

[Resolved] Automatically display categories, their sub-categories and pages

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

Problem:
How to display category - subcategory and post using custom taxonomy and custom post type

Solution:
You need to create two views one for custom taxonomy and one view to display the post belongs to nth category.

You can find proposed solution, in this case, with the following reply:
=> https://toolset.com/forums/topic/automatically-display-categories-their-sub-categories-and-pages/#post-625564

Relevant Documentation:

This support ticket is created 6 years, 9 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
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Kolkata (GMT+05:30)

This topic contains 7 replies, has 2 voices.

Last updated by tomaszB-4 6 years, 9 months ago.

Assisted by: Minesh.

Author
Posts
#625467

Hi!

I want to make automatically display categories, their sub-categories and pages.
The reason is that I have a lot of categories and sub-categories and my hierarchy has 2 or more levels.
For example:

Category 1
- SubCategory 1.1
- SubCategory 1.2
Category 2
- SubCategory 2.1
- - SubCategory 2.1.1
- - SubCategory 2.1.2
- - - SubCategory 2.1.2.1
- - - SubCategory 2.1.2.2
- SubCategory 2.2
- - SubCategory 2.2.1
- - SubCategory 2.2.2

Pages are displayed only at the end of the hierarchy, i.e. in the last subcategory.

I have read this post, but I missed something because it does not work as it should.
https://toolset.com/forums/topic/display-sub-category-on-top-level-display-posts-on-lowest-level/#post-437503

What I did:
1. I created custom post type and taxonomy
2. I made view "kategorie" for main display:

Content:
Taxonomy

Filter:
Taxonomy parent filter: Parent is the term of the current taxonomy archive page

Loop Editor:
[wpv-items-found]
<!-- wpv-loop-start -->
<wpv-loop>
[wpv-taxonomy-link]<br />
</wpv-loop>
<!-- wpv-loop-end -->
[/wpv-items-found]
[wpv-no-items-found]
[wpv-view name="lista-produktow"]
[/wpv-no-items-found]

3. I made view "lista-produktow" the purpose of which is to display pages.

Post type:
produkty

Query Filter:
I choose random filters but without success.

4. I made view for Archives,
and put in there view "kategorie".

Effect is:
Categories and subcategories are duplicated and pages are not visible.

I will be very grateful for every hint.

#625564

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

Well - To be able to keep navigating all the way through the categories - sub-categories until we reach the page/post level, I have following solution for you. I hope this will work for you as well.

1)
Create a taxonomy view. Name this view as "top-category-view"
=> Uncheck the checkbox "Don't show empty terms" at section "Query Options"
=> Add following code to it's loop output section [You can add fields as you wanted to loop output]:

<wpv-loop>
<a href="?taxonomy_id=[wpv-taxonomy-id]" target="_self" >
    <h3 class="image-link-text">[wpv-taxonomy-title]</h3>
</a>  
</wpv-loop>

2)
Add following code to your current theme's functions.php file:

add_filter( 'wpv_filter_taxonomy_query', 'filter_application_by_experience_years_func', 99, 4 );
function filter_application_by_experience_years_func($tax_query_settings, $view_settings, $view_id) {
	if($view_id == 9999){
		(!isset($_GET['taxonomy_id']))? $tax_query_settings['child_of']=0:$tax_query_settings['child_of'] = $_GET['taxonomy_id'];
	}
    return $tax_query_settings;
}
add_filter( 'wpv_filter_taxonomy_post_query', 'prefix_check_items', 20, 4 );
function prefix_check_items( $items, $tax_query_settings, $view_settings, $view_id ) {
	if($view_id == 9999){
		
		foreach( $items as $item ) {
			$children = get_term_children( $item->term_id, 'product_cat');
			
			if(count($children)==0){
				$product_page=$_SERVER['REQUEST_SCHEME']."://".$_SERVER['HTTP_HOST']."/listing-page/";
				$item->name = '<a href="'.$product_page.'?wpvproductcat='.$item->slug.'" target="_self" >'.$item->name.'</a>';
			}else{
				$item->name = '<a href="?taxonomy_id='.$item->term_id.'" target="_self" >'.$item->name.'</a>';
			}	
		}
    }
    return $items;
}

Where:
== Replace '9999' with the view ID of "top-category-view" created at step #1

3)
Create a new page and add "top-category-view" view to list categories

4)
Create a new View, to list your CPT pages/post namely "category-post-list"
=> Add new query filter for your taxonomy "Categories" and save it

For example:

Select posts with taxonomy: Categories slug in one of those set by the URL parameter wpvproductcat
eg. <em><u>hidden link</u></em> 

=> add fields to display your product as required at "loop output" section

5)
Create a new page namely "Listing page" and add view "category-post-list" created at step #4

I hope this way you can achieve your goal to navigate to endless subcategories and at the end display products that belongs to the last subcategory.

#625589

Hi Minesh!
Thank you very much for such an advanced solution.

I did everything you wrote and checked twice.
I also checked the view ID. This is 111 and it was given in two places in the functions.php code.

Unfortunately, the effect is that the main category has appeared, but you can't open it.
You may see it on:
domain/category/

Domain is this from admin access form.

Thanks and I calmly wait for further help,
because I will not figure it out by myself 🙂

#625594

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Well - I need FTP access in order to check whats going wrong there. Could you please send me FTP access details.

*** Please make a FULL BACKUP of your database and website.***
I would also eventually need to request temporary access (WP-Admin and FTP) to your site. Preferably to a test site where the problem has been replicated if possible in order to be of better help and check if some configurations might need to be changed.

I would additionally need your permission to de- and re-activate Plugins and the Theme, and to change configurations on the site. This is also a reason the backup is really important. If you agree to this, please use the form fields I have enabled below to provide temporary access details (wp-admin and FTP).

I have set the next reply to private which means only you and I have access to it.

#625602

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Ok - I think not needed - It was a cache issue and I've added the view as given under:

[wpv-view name="top-category-view" cached="off"]

Also, I've made following change with following line where I replace the 'pro' that is your taxonomy slug:

 $children = get_term_children( $item->term_id, 'pro');

I can see that its working fine now. Could you please confirm.

#625607

Thanks for hiper fast action and answer! 🙂
Yes, it works like I want. Thank you very much.

Only one thing remains to be improved.
It's about the name of the page in H1. In all categories is "Category" and on product listing page "Listing page".
So it is the name of the page displaying the view.
The same is with the title meta.

I wonder if there is any way to change H1 and meta title into a name from taxonomy.
This not only matters to Google,
but also for the client browsing the website.
Thanks to these names, he will get a better idea of where he is, which pages he looks at.

Maybe I should make a separate template for this page and remove the default H1, and insert H1 with some code?
But that would solve the problem with H1, and what about the title meta?

#625612

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Glad to know that now it works and the solution I shared help you to resolve your issue.

As your original issue is resolved - May I kindly ask you to open a new ticket with your each new question. This will help other users searching on the forum.

Even you can assign it new ticket to me and add the old ticket link as reference - I'm happy to help.

Thank you for understanding.

#625615

I wrote a new post:

https://toolset.com/forums/topic/change-h1-and-meta-title/

And thanks for super help!