Categories

Friday, July 26, 2024
#919814419350 therichposts@gmail.com
WoocommerceWoocommerce HooksWordpress

How to display Primary Category in the WordPress Permalink?

HOW TO DISPLAY PRIMARY CATEGORY IN THE WORDPRESS PERMALINK?

To display the primary category in the permalink of your WordPress posts, you’ll need to modify the permalink structure to include the category. This involves a bit of setup and customization within your WordPress site. Here’s how you can do it:

Primary category link inside permalink
HOW TO DISPLAY PRIMARY CATEGORY IN THE WORDPRESS PERMALINK?

1. Set the Permalink Structure

First, you need to adjust the permalink settings to include the category:

  • Go to your WordPress dashboard.
  • Navigate to Settings > Permalinks.
  • Select the Custom Structure option.
  • Add /%category%/%postname%/ to the field to ensure the URL structure includes the category followed by the post name.

2. Install a Plugin to Handle Primary Categories

By default, WordPress does not support the concept of a “primary” category for posts that have multiple categories. To implement this, you can use a plugin like Yoast SEO, which allows you to specify a primary category for each post. After installing Yoast SEO:

  • Edit the post where you want to set a primary category.
  • In the categories section, you’ll see an option to make one of the categories the primary one (provided by Yoast SEO).

3. Custom Function for Primary Category in Permalink

If you prefer not to use a plugin or need more customized behavior, you can add some code to your theme’s functions.php file to override the default category used in permalinks. Here’s an example code snippet:

add_filter('post_link', 'custom_primary_category_permalink', 10, 3);
add_filter('post_type_link', 'custom_primary_category_permalink', 10, 3);

function custom_primary_category_permalink($permalink, $post_id, $leavename) {
    if (strpos($permalink, '%category%') === FALSE) return $permalink;

    // Get post
    $post = get_post($post_id);
    if (!$post) return $permalink;

    // Get primary category
    $primary_category = get_the_terms($post->ID, 'category')[0];  // Adjust to select your primary category logic
    if ( !is_wp_error($primary_category) && !empty($primary_category) && is_object($primary_category) ) {
        $category_slug = $primary_category->slug;
    } else {
        $default_category = get_term_by('id', get_option('default_category'), 'category');
        $category_slug = $default_category->slug;
    }

    return str_replace('%category%', $category_slug, $permalink);
}

4. Refresh Permalinks

After making changes, especially if you’ve added or modified code:

  • Go back to Settings > Permalinks in the WordPress dashboard.
  • Just click Save Changes without altering anything to refresh the permalink structure.

This setup should help you incorporate the primary category into your WordPress permalinks. Make sure to test the changes in a staging environment before applying them to your live site to avoid any disruptions.

Jassa

Thanks

therichpost
the authortherichpost
Hello to all. Welcome to therichpost.com. Myself Ajay Malhotra and I am freelance full stack developer. I love coding. I know WordPress, Core php, Angularjs, Angular 14, Angular 15, Angular 16, Angular 17, Bootstrap 5, Nodejs, Laravel, Codeigniter, Shopify, Squarespace, jQuery, Google Map Api, Vuejs, Reactjs, Big commerce etc.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.