1
votes

I'm using the permalinks on my Wordpress posts. My permalink code is /%category%/%postname%/ - so links include category name. My problem is, I'm using some categories to group my posts. For example: featured, recommendation of the moderators etc... I want these categories to appearing in the links. Is this possible and if yes how can it be done?

3
If you're using the category slug it should already display... but it sounds like you're using some type of template file to "group your posts". If you could provide that code, or start there...Tim Hallman
@TimHallman My some posts have 2 or 3 category. I want exclude some categories from permalinks.Fatih SARI
Okay, gotcha... probably need to filter permalinks then. I'll give an answer below with an example.Tim Hallman

3 Answers

1
votes
add_filter( 'post_link', 'filter_permalink_categories', 10, 3 );

function filter_permalink_categories( $permalink, $post, $catname) {

    if( $post->filter == $catname ) {

        $permalink = str_replace( $catname, '', $permalink );

    }

    return $permalink;

}

This might not be exactly what you need, but should give you an idea how to filter the permalinks.

Also, you may want to check out the category_link filter. Sorry, Wordpress CODEX is silent heres the link though

1
votes

I change other category of post has.

$disallow = array("category-slug-1", "category-slug-2");
function selectCategory($postID){
    global $disallow;
    $cats=wp_get_post_categories($postID);
    foreach ($cats as $key => $value) {
        $current = get_category($value);
        if(!in_array($current->slug, $disallow)){
            return $current->slug;
        }
    }
}

And replace in permalink with this:

add_filter( 'post_link', 'filter_permalink_categories', 10, 3 );
function filter_permalink_categories( $permalink, $post, $catname) {
    global $disallow;
    foreach ($disallow as $key => $value) {
        $permalink = str_replace( $disallow, selectCategory($post->ID), $permalink );
    }
    return $permalink;

}

Please remember save permalink setting from dashboard after these.

1
votes

However, the WordPress team have been making improvements on this, and now it's possible to include not just one, but multiple categories inside a URL. In this tutorial, we'll show you how to make that happen.

WordPress Permalinks with Multiple Categories

Or

function multiple_category_post_link($url = '')
{  
  // check permalink structure for the required construct; /%category%/%postname%/ 
  if (strrpos(get_option('permalink_structure'), '%category%/%postname%') !== false)
  {
    // get the current post
    global $post, $wp_query;

    // prepare variables for use below
    $post_id = $cat_id = 0;
    $new_url = '';

    // for categories
    if (is_category()) 
    {
      // remember current category and post
      $cat_id = get_query_var('cat'); 
      $post_id = $post->ID;

      // add the post slug to the current url
      $new_url = $_SERVER['REQUEST_URI'] . $post->post_name;   
    }

    // for single posts 
    else if (is_single()) 
    {
      // last part in the 'category_name' should be the slug for the current category
      $cat_slug = array_pop(explode('/', get_query_var('category_name')));
      $cat = get_category_by_slug($cat_slug);

      // remember current category and post
      $post_id = $wp_query->post->ID;     
      if ($cat) $cat_id = $cat->cat_ID;

      // replace the slug of the post being viewed by the slug of $post
      $new_url = str_replace('/' . get_query_var('name'), '', $_SERVER['REQUEST_URI']) . $post->post_name;
    } 

    if ($post_id > 0 && $cat_id > 0 && !empty($new_url))
    {
      // make sure categories match!
      foreach(get_the_category($post_id) as $cat)
      {
        if ($cat->cat_ID == $cat_id)
        {
          $url = $new_url;
          break;
        } 
      }  
    }    
  }   

  // always return an url!
  return $url;
} 
add_filter('post_link', 'multiple_category_post_link');