Get top parent category of child category wordpress năm 2024

hello everybody I would like to visualize through the hook the child categories of the category that I am displaying.

I followed this guide, (https://generatepress.com/forums/topic/displaying-sub-categories-on-parent-category-page/) but it’s not perfect for what I want to do.

In my case, I would like the child categories to be displayed this way when I view the computer category.

Accessori

Hardware > Componenti > Configurazioni pc gaming > monitor > pc portatili e desktop

Wiki pc > installare sistema operativo > ottimizzare il pc > sicurezza informatica > etc…

practically like Aranzulla does in his blog https://www.aranzulla.it/computer

September 6, 2019 at 4:29 pm

Tom

Lead Developer

Lead Developer

Hi there,

Just so I understand – are you wanting to display a list of child category items in your sidebar when viewing a parent category?

If so, maybe this is worth checking out?: https://wordpress.org/plugins/sub-categories-widget/

September 7, 2019 at 1:08 am

simone

hello, that plugin is not good because you have to set the parent category, while I would like it to be displayed automatically

September 7, 2019 at 5:00 am

David

Staff

Customer Support

Hi there,

can you confirm that you want to display the list of Child Categories in the Sidebar ?

September 7, 2019 at 5:44 am

simone

Hi, I’d prefer below the category title like it is now (via hook), but if it’s really not possible in the sidebar.

I wish that when I click on a parent category the sub-categories are displayed in this way.

I give a practical example.

If I click on the computer category from home, I want the sub-categories to be displayed in this way.

  • Accessori
  • Hardware
    • –Componenti
      –Configurazioni pc gaming  
      –Monitor  
      –Pc Portatili e Desktop
  • –Wiki Pc
    • -Installare sistema operativo
      \-Ottimizzare il pc  
      \-Sicurezza informatica  
      \-Utility Pc –

if I then click on the Hardware category I want only its sub-categories to be displayed -Componenti -Configurazioni pc gaming -Monitor -Pc Portatili e Desktop

September 7, 2019 at 3:46 pm

Tom

Lead Developer

Lead Developer

I wonder if something like this would do it?: https://wordpress.stackexchange.com/a/33656

You would add that to a Hook Element, and set it to the after_archive_title hook.

September 8, 2019 at 12:56 am

simone

good morning, it doesn’t work.

I am currently using this code, but it shows me only the higher categories, but not the daughters

taxonomy, array( 'parent' => $term->term_id, 'hide_empty' => false ) ); if ( $children ) { foreach( $children as $subcat ) { echo '' . $subcat->name . ' - '; } } ?>

September 8, 2019 at 9:27 am

Tom

Lead Developer

Lead Developer

When I go to the Computer category, I see these three links:

Accessori – Hardware – Wiki Pc

Those seem to be child categories of the “Computer” category, no?

September 8, 2019 at 9:31 am

simone

it is correct, but I would like them to see their sub-categories as in the aranzulla site.

September 8, 2019 at 4:13 pm

Tom

Lead Developer

Lead Developer

So if those child categories have child categories, you want to display them as well? How deep are you wanting to go?

How do I get child categories of parent in WordPress?

To get child categories you can use following code. $category = get_queried_object(); // this is for getting the parent category on archive or any place the category object is called. $categories=get_categories( array( 'parent' => $category->term_id, 'hide_empty' => false ) );

How do I get only parent category in WordPress?

php wpb_get_parent_terms(); ?> This code will display your parent category alone. If you have multiple categories that are the parent or standalone categories, then all such categories will also be displayed. The code snippet will work for all other taxonomies as well.

How do I find sub categories by parent category ID in WordPress?

You'd use the WP_Term_Query class to get child category terms, passing the current term's ID as the “parent” arg. This will return found terms in an array, which other PHP code can parse into the desired HTML output.

How do I get the primary category in WordPress?

Step 1: Open the Post/Page/Product in your WordPress dashboard. Step 2: Choose more than one category from the list of available categories in the sidebar of your editing post screen. Step 3: Choose the Category radio button to set your primary category in the case of Classic Editor.