Skip to main content
deleted 45 characters in body
Source Link
Pieter Goosen
  • 55.4k
  • 23
  • 115
  • 209

if( term_exists(get_the_title(), 'category' ))

if( term_exists(get_the_title(), 'category' ))

$cat = get_term_by("slug", $post->post_name, "category");

$cat = get_term_by("slug", $post->post_name, "category"); 
              if($cat->parent !== 0) {
             if($cat->parent !== 0) {
    $catID = $cat->parent;
                } else {
                    $catID = $cat->term_id;
                }  
              $args = array(
              $args = array(
                'type'                      => 'post',
                                'child_of'                  => $catID,
                                'hide_empty'                  => false,
                                'hierarchical'              => true,
                                'exclude'                   => '1'
                            );
                $categories = get_categories($args);
              foreach($categories as $categorie) {
             foreach($categories as $categorie) {
    if($categorie->slug == $post->post_name) {
                        $clasa = "class='current'";
                    } else {
                        $clasa = "";
                    }
                    echo "<a ".$clasa." href='".home_url("/").$slug."/".$categorie->slug."'>".$categorie->name."</a>";
                }

if( term_exists(get_the_title(), 'category' ))

$cat = get_term_by("slug", $post->post_name, "category");

              if($cat->parent !== 0) {
                    $catID = $cat->parent;
                } else {
                    $catID = $cat->term_id;
                }  
              $args = array(
                                'type'                      => 'post',
                                'child_of'                  => $catID,
                                'hide_empty'                  => false,
                                'hierarchical'              => true,
                                'exclude'                   => '1'
                            );
                $categories = get_categories($args);
              foreach($categories as $categorie) {
                    if($categorie->slug == $post->post_name) {
                        $clasa = "class='current'";
                    } else {
                        $clasa = "";
                    }
                    echo "<a ".$clasa." href='".home_url("/").$slug."/".$categorie->slug."'>".$categorie->name."</a>";
                }
if( term_exists(get_the_title(), 'category' ))
$cat = get_term_by("slug", $post->post_name, "category"); 
if($cat->parent !== 0) {
    $catID = $cat->parent;
} else {
    $catID = $cat->term_id;
}  
$args = array(
                'type'                      => 'post',
                'child_of'                  => $catID,
                'hide_empty'                  => false,
                'hierarchical'              => true,
                'exclude'                   => '1'
            );
$categories = get_categories($args);
foreach($categories as $categorie) {
    if($categorie->slug == $post->post_name) {
        $clasa = "class='current'";
    } else {
        $clasa = "";
    }
    echo "<a ".$clasa." href='".home_url("/").$slug."/".$categorie->slug."'>".$categorie->name."</a>";
}
Source Link

Did this before for a website. Here's what i did:
Checked if there is a category with the same name as the title:

if( term_exists(get_the_title(), 'category' ))

If there was one then I got the category by slug:

$cat = get_term_by("slug", $post->post_name, "category");

Checked if category had parent or was a parent:

              if($cat->parent !== 0) {
                    $catID = $cat->parent;
                } else {
                    $catID = $cat->term_id;
                }  

Then retrieved categories by said parent:

              $args = array(
                                'type'                      => 'post',
                                'child_of'                  => $catID,
                                'hide_empty'                  => false,
                                'hierarchical'              => true,
                                'exclude'                   => '1'
                            );
                $categories = get_categories($args);

Then a simple loop with links:

              foreach($categories as $categorie) {
                    if($categorie->slug == $post->post_name) {
                        $clasa = "class='current'";
                    } else {
                        $clasa = "";
                    }
                    echo "<a ".$clasa." href='".home_url("/").$slug."/".$categorie->slug."'>".$categorie->name."</a>";
                }

Hope this is what you are looking for.