3

I have some categories like so:

/europe/postname
/usa/postname

For various reasons I would like to rewrite these to be:

/news/view/postname

I've tried a few re-direct plugins but not had any success. Is this possible?

7
  • /europe/postname, postname? are you talking about post rewrite rules? or category's?
    – Jesse
    Apr 17, 2014 at 13:02
  • @jesse I think it is the category rewrite I need as the postname wil be the same. So /europe/ becomes /news/view/ Apr 17, 2014 at 13:05
  • and /usa/ will become /news/view/ too? maybe you should simply set the Custom Structure to "/news/view/%postname%/"
    – Jesse
    Apr 17, 2014 at 13:08
  • Yes that's right. Unfortunately I can't do that as the custom structure is used for other content too. Apr 17, 2014 at 13:10
  • Sorry, I don't know how to make rewrite rules just for some categories. if you just want to redirect /news/view/postname to the post, I can help.
    – Jesse
    Apr 17, 2014 at 13:20

1 Answer 1

1

It can be done by writing a code on post_link filter. This filter allows you to change the final post link.

Since you are showing categories in your permalink, so I am assuming that your permalink structure contains %category% in it.

Below code will help you to get links the way you want.

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

function wdm_change_category_permalink_structure( $post_link, $post, $leavename ) {

    $array_of_cats_to_be_replaced = array( 'europe', 'usa' ); //array of category slugs. Add more slugs here

    $get_permalink_structure = get_option( 'permalink_structure' );

    if ( strpos( $get_permalink_structure, '%category%' ) !== false ) {

        $cats = get_the_category( $post->ID ); //get categories assocaited with the post

        if ( $cats ) {

            usort( $cats, '_usort_terms_by_ID' ); // order by ID  

            $category_object = get_term( $cats[0], 'category' ); //Take first category

            $category = $category_object->slug; //Get slug of category

            if ( in_array( $category, $array_of_cats_to_be_replaced ) ) { //If category we want is in the array
                $rewritecode = array(
                    '%year%',
                    '%monthnum%',
                    '%day%',
                    '%hour%',
                    '%minute%',
                    '%second%',
                    $leavename ? '' : '%postname%',
                    '%post_id%',
                    '%category%',
                    '%author%',
                    $leavename ? '' : '%pagename%',
                );
                $rewritereplace = array(
                            $date[0],
                            $date[1],
                            $date[2],
                            $date[3],
                            $date[4],
                            $date[5],
                            $post->post_name,
                            $post->ID,
                            'news/view', //Replace category with news/view
                            $author,
                            $post->post_name,
                );

                $post_link = home_url( str_replace( $rewritecode, $rewritereplace, $get_permalink_structure ) );
                $post_link = user_trailingslashit( $post_link, 'single' );
            }
        }
    }
    return $post_link;
}

Hope it helps :)

1
  • Thanks for this I will look into this for other projects: it looks interesting. I ended up dealing with this in a different way in the end by using a different category structure. Jul 28, 2014 at 19:26

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.