$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>htaccess - Conditional Permalink based on category?|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

htaccess - Conditional Permalink based on category?

matteradmin10PV0评论

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.

Post a comment

comment list (0)

  1. No comments so far