Displaying or get primary category on WordPress in both admin and front-end pages can be a challenge because a post could belong to several different categories and if the theme has to display multi categories for each post in a list, it could get messy. This is where a primary category comes in handy.
Yoast SEO is a great plugin for managing general SEO, it also provides a lot of features that can easily to be tweaked. If a blog post has multiple categories, and want to designate the first selected category as the “Primary Category”. here is tutorial demonstrates step by step how to get a primary category on WordPress.
Prerequisites
Before starting with this guide, make sure you have permission access to WordPress project root directory and had installed Yoast plugin
Step 1: Creating Function in Inc Directory of WP Theme
We need to create a file of function to handle selecting this primary category in inc directory and we can hook in and grab it by calling with the function
Go to WordPress root directory ➝ wp-content ➝ themes ➝ YOUR THEME NAME ➝ inc
Then create a file, for example it’s called primary-category.php
and add or copy paste the following code
<?php
function get_primary_category( $post = 0 ) {
if ( ! $post ) {
$post = get_the_ID();
}
// SHOW YOAST PRIMARY CATEGORY, OR FIRST CATEGORY
$category = get_the_category( $post );
$primary_category = array();
// If post has a category assigned.
if ($category){
$category_display = '';
$category_slug = '';
$category_link = '';
$category_id = '';
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( $post ) );
$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_slug = $category[0]->slug;
$category_link = get_category_link( $category[0]->term_id );
$category_id = $category[0]->term_id;
} else {
// Yoast Primary category
$category_display = $term->name;
$category_slug = $term->slug;
$category_link = get_category_link( $term->term_id );
$category_id = $term->term_id;
}
}
else {
// Default, display the first category in WP's list of assigned categories
$category_display = $category[0]->name;
$category_slug = $category[0]->slug;
$category_link = get_category_link( $category[0]->term_id );
$category_id = $term->term_id;
}
$primary_category['url'] = $category_link;
$primary_category['slug'] = $category_slug;
$primary_category['title'] = $category_display;
$primary_category['id'] = $category_id;
}
return $primary_category;
}
?>
Now we have a function get_primary_category()
for getting the primary category with return or echo to use in WordPress theme.
Step 2: Modify Functions File
A functions.php
or the theme functions file is a template used by WordPress themes. We need to include primary-category.php
into functions.php
to get automatically loaded in both admin and front-end pages of a WordPress site.
Go to WP root directory ➝ wp-content ➝ themes ➝ YOUR THEME NAME ➝ functions.php
Then add or copy paste the following code into function.php
require get_template_directory() . '/inc/primary-category.php';
Now a function get_primary_category()
can be grab in both admin and front-end pages of a WordPress site.
Step 3: Using Function in Admin or Front-end Site
To display WordPress primary categories it’s now simply call a function get_primary_category();
Let’s we test it in a single page WordPress theme
echo '<pre>'.print_r(get_primary_category(get_the_ID()), true).'</pre>';
Or
echo '<pre>'.print_r(get_primary_category(), true).'</pre>';
You will see output on the browser
Array
(
[url] => URL...
[slug] => Primary Category Slug......
[title] => Primary Primary Title.....
[id] => Primary Categery ID
)
If this tutorial could help you, please rate above Star button rating and share to help others find it! Feel free to leave a comment below.