0

As the title says, I want to create a different permalink structure for posts belonging to a certain category, in my case, the name is "ICO". I've found this piece of code on StackExchange, but it doesn't really do anything for me, contrary to the OPs feedback.

The code is:

//Rewrite URLs for "ICO" category
add_filter( 'post_link', 'custom_permalink', 10, 3 );

function custom_permalink( $permalink, $post, $leavename ) {
// Get the category for the post
$category = get_the_category($post->ID);
if (  !empty($category) && $category[0]->cat_name == "ICO" ) {
    $cat_name = strtolower($category[0]->cat_name);
    $permalink = trailingslashit( home_url('/'. $cat_name . '/' . $post->post_name .'/' ) );
}
return $permalink;
}

add_action( 'init', 'custom_rewrite_rules' );
function custom_rewrite_rules() {
add_rewrite_rule(
    'ico/([^/]+)(?:/([0-9]+))?/?$',
    'index.php?category_name=ico&name=$matches[1]&page=$matches[2]',
    'top' // The rule position; either 'top' or 'bottom' (default).
);
}

All posts from that category, present on the homepage do not have their URL changed, the default URL structure is the same www.site.com/post_name/

However, I noticed that on some widgets, in a list with posts from that category, some of the posts have their URL changed, some do not. There isn't any difference between them, they have the same categories. Even if I go on that URL /ico/post_name/ it works and if I delete /ico/ and go on the default permalink structure, it works as well, making it a duplicate URL for the same post.

What have I done wrong?

Edit: I also tried this piece of code, same effect, not working

add_filter( 'post_link', 'custom_permalink', 10, 3 );
function custom_permalink( $permalink, $post, $leavename ) {
    // Get the categories for the post
    $category = get_the_category($post->ID); 
    if (  !empty($category) && $category[0]->cat_name == "ICO" ) {
        $cat_name = strtolower($category[0]->cat_name);
        $permalink = trailingslashit( home_url('/'. $cat_name . '/' . $post->post_name .'/' ) );
    }
    return $permalink;
}
add_action('generate_rewrite_rules', 'custom_rewrite_rules');
function custom_rewrite_rules( $wp_rewrite ) {
    // This rule will will match the post id in %postname%-%post_id% struture
    $new_rules['ico/^([^/]*)/?'] = 'index.php?p=$matches[1]';
    $wp_rewrite->rules = $new_rules + $wp_rewrite->rules;
    return $wp_rewrite;
}
12
  • did you flush your rewrite rules afterwards? resaved a post that is inside that specific category..?
    – honk31
    Sep 3, 2018 at 14:18
  • actually I did, nothing happed. Moreover, the posts that have the url changed with the /ico/ category, when I click on them, instead of taking me to the post page, displays the featured image attached to that post.
    – vexx
    Sep 3, 2018 at 15:53
  • as resaving, didn't think of doing that..I'll do that now, I thought this rule works as changing the permalinks from WP directly, you don't need to resave all posts.
    – vexx
    Sep 3, 2018 at 15:58
  • I resaved a bunch of articles, did a permalink save, added flush_rewrite_rules(); to the function in function.php...nothing works. I'm clearly missing something or there's something wrong with the code.
    – vexx
    Sep 3, 2018 at 16:27
  • so, you paste your code and edit your post yadayada all works fine, when you hit save, the permalink below the title is all good, has ico inside, the issue comes with the rewrite. correct?
    – honk31
    Sep 3, 2018 at 16:32

2 Answers 2

0

here is a solution to search through all categories, not only the first one.

add_filter('post_link', 'custom_permalink', 10, 2);
function custom_permalink( $permalink, $post ) {
    // Get the categories for the post
    $categories = get_the_terms($post->ID, 'category'); 
    if (!empty($categories)) {
        foreach ($categories as $category) {
            if ($category->cat_name == "ICO") {
                $cat_name = strtolower($category->cat_name);
                $permalink = trailingslashit( home_url('/' . $cat_name . '/' . $post->post_name . '/' ) );
                break; //we found ico, so exit foreach immediately
            }
        }
    }
    return $permalink;
}
1
  • Tried it now, doesn't do anything, not even the "failed" rewrite the first iteration did. I tried using both custom rewrite rules, the original and the one you specified after, same effect.
    – vexx
    Sep 7, 2018 at 14:08
0

I changed the main functionn to this:

add_filter( 'post_link', 'custom_permalink', 10, 3 );

function custom_permalink( $permalink, $post, $leavename ) {
// Get the category for the post
$category = get_the_category($post->ID);
$cat_id = get_cat_ID ( $category );
if (  !empty($category) && has_category('ICO',$post->ID) ) {
    $permalink = trailingslashit( home_url('/ico/' . $post->post_name .'/' ) );
}
return $permalink;
}

This changes the URL correctly to all the posts from the ICO category. However, if I go to any of these articles, it returns a 404, which doesn't make much sense.

1
  • Ok, rushing this, I forgot to regenerate permalinks and now it works. However, one problem still remains, if I delete the /ico/ portion of the URL, the link works aswell, pointing to the same page. Not sure how to fix this.
    – vexx
    Sep 10, 2018 at 13:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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