htaccess - Conditional Permalink based on category?

admin2025-06-05  6

There have been unanswered questions about this here and in the WordPress support forum.

Is it possible to have a conditional Permalink structure for all posts in a category?

for example, %year%/%category%/postname% for a given category, but for the other permalinks have a different structure (without the year for example)?

I'm not very efficient with htaccess, I can use some help please.

Thanks!

There have been unanswered questions about this here and in the WordPress support forum.

Is it possible to have a conditional Permalink structure for all posts in a category?

for example, %year%/%category%/postname% for a given category, but for the other permalinks have a different structure (without the year for example)?

I'm not very efficient with htaccess, I can use some help please.

Thanks!

Share Improve this question asked Nov 19, 2014 at 8:52 RoundsRounds 1919 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

Answering My own question..

That was easier than I though.. I used the post_link filter to modify the permalink in the following way..

function custom_permalink( $permalink, $post ) {
    // Get the categories for the post
    $post       = get_post( $post_id );
    $category   = get_the_category( $post_id ); 
    $post_year  = mysql2date("Y", $post->post_date);
    $target_cat = 6; // Category we'd like to change permalink for

    if ( empty( $post_year ) ) return $permalink;

    if ( $category[0]->cat_ID == $target_cat ) {
        $permalink = trailingslashit( home_url( $category[0]->slug . '/' .$post_year .'/' . $post->post_name . '/'  ) );
    }

    return $permalink;
}

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

And of course, flushing permalinks by visiting the settings > permalinks page.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1749136402a316668.html

最新回复(0)