Home > Mobile >  Rank math Category Title
Rank math Category Title

Time:10-15

I want to set the category title but I couldn’t do it.

My site has 1 main category and 2 subcategories. There are thousands of categories like this.

Main Category: Iphone -Sub Category: 13 Pro Max –Subcategory of Subcategory: 2022

The title I want to create: 2022 Iphone 13 Pro Max

Sorting Name of the subcategory of the Subcategory – Name of the Main Category – Name of a subcategory

How can I make this structure for all categories? It is impossible to do it manually because it is an archive site and there are thousands of categories like this. Please tell me this is possible with rank math.

Thanks in advance for your help.

CodePudding user response:

Lets break it down:

first you want to get the Parent Category of the given post or you can define it manually.

Within the loop

$parent = get_the_category();

Then we'll get the first category

$parent = get_category($parent[0]->category_parent);

then from there get the child

$child = get_categories( array( 
   'parent' => $parent->term_id 
) );
$child = get_category($child[0]->term_id);

and the grandchild

$grandchild = get_categories( array( 
    'parent' => $child->term_id 
) );
$grandchild = get_category($grandchild[0]->term_id);

then echo it out

echo get_cat_name($grandchild->term_id) . ' ' . get_cat_name($parent->term_id). ' ' . get_cat_name($child->term_id);

Combined

$parent = get_the_category(); 
$parent = get_category($parent[0]->category_parent);
        
$child = get_categories( array( 
   'parent' => $parent->term_id 
) );
$child = get_category($child[0]->term_id);
        
$grandchild = get_categories( array( 
    'parent' => $child->term_id 
) );
$grandchild = get_category($grandchild[0]->term_id);

echo get_cat_name($grandchild->term_id) . ' ' . get_cat_name($parent->term_id). ' ' . get_cat_name($child->term_id);

This should output

2022 iPhone 13 Pro Max
  • Related