I understand Wordpress concept "primary category", but I do not understand how to fetch the name of the highest category in the hierarchy for a specific post?

Having this code:

$category_obj = get_the_category( $post_id );
$category_name = $category_obj[0]->cat_name;                   

would fetch the category's name of the post, but this category is only the first category listed for the post and that category is not always the primary category.

I'm using this outside of "the loop".

  • the is no such thing as primary category in wordpress – Mark Kaplun Jun 10 at 16:45
  • @MarkKaplun - the first category then :-) Sorry for not knowing the terminology correctly. – bestprogrammerintheworld Jun 12 at 6:22
  • the problem is there is no such thing as "first" in the context of post terms. Most likely whatever will be "first" is just either what was created first, or based on sort by category name, neither will usually be what you will consider as "primary", at least not always – Mark Kaplun Jun 12 at 7:04
  • based on your answer, maybe you meant the highest category in the hierarchy? or maybe just the one used in the url for a specific permalink structure? – Mark Kaplun Jun 12 at 7:09
  • @MarkKaplun - Yes I meant highest category in the hierarchy. – bestprogrammerintheworld Jun 13 at 7:18

I figured this out. I couldn't find any core functionaliy for this in Wordpress, but to retrieve the primary category you could use the get permalink function:

$site_url = get_site_url();
$post_slug = get_permalink( $post_id );
$post_slug_remove_siteurl = str_replace( $site_url, '', $post_slug);                           
$category_from_slug = explode ('/', $post_slug_remove_siteurl);
$category_name = $category_from_slug[1];

This requires you to have category included in your permalink. The reason why this is possible is that when changing primary category the permalink is changed as well:

<site-url> / <primary category> / <post name>

Please tell me if there are any better of achieving this!

Display Primary Category (Yoast's WordPress SEO)

<?php
            // SHOW YOAST PRIMARY CATEGORY, OR FIRST CATEGORY
            $category = get_the_category();
            $useCatLink = true;
            // If post has a category assigned.
            if ($category) {
                $category_display = '';
                $category_link = '';
                if (class_exists('WPSEO_Primary_Term')) {
                    // Show the post's 'Primary' category, if this Yoast feature is available, & one is set
                    $wpseo_primary_term = new WPSEO_Primary_Term('category', get_the_id());
                    $wpseo_primary_term = $wpseo_primary_term->get_primary_term();
                    $term = get_term($wpseo_primary_term);
                    if (is_wp_error($term)) {
                        // Default to first category (not Yoast) if an error is returned
                        $category_display = $category[0]->name;
                        $category_link = get_category_link($category[0]->term_id);
                    } else {
                        // Yoast Primary category
                        $category_display = $term->name;
                        $category_link = get_category_link($term->term_id);
                    }
                } else {
                    // Default, display the first category in WP's list of assigned categories
                    $category_display = $category[0]->name;
                    $category_link = get_category_link($category[0]->term_id);
                }
                // Display category
                if (!empty($category_display)) {
                    if ($useCatLink == true && !empty($category_link)) {
                        echo '<span class="post-category">';
                        echo '<a href="' . $category_link . '">' . htmlspecialchars($category_display) . '</a>';
                        echo '</span>';
                    } else {
                        echo '<span class="post-category">' . htmlspecialchars($category_display) . '</span>';
                    }
                }
            }
            ?>

Your Answer

 
discard

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.