Php – Custom URL rewrite in wordpress

blogsMySQLPHPurl-rewritingWordpress

I am trying to achieve a custom URL structure with WordPress. Basically, my site functions as my blog and my portfolio. I want to have an "Articles" section and a "Portfolio" section. I want the articles to display on the Articles page, and the portfolio on the Portfolio page. Should be easy, except on top of that I want custom URLs…

I want the URLs setup like this:

Articles page:
http://mydomain.com/discusses

Specific article:
http://mydomain.com/discussed/%postname%

Portfolio page:
http://mydomain.com/does

Specific design:
http://mydomain.com/designed/%postname%

I can figure out how to do only one rewrite of this type, but WP doesn't have built in functionality to do both. I'm just wondering where I should start or what I should do…

The problem also becomes, I want to retain category functionality in both areas, so maybe it needs to be achieved with Custom Fields? and add a new rewrite rule based on the custom field of a post? I have idea where to start…

Thanks for any help you can give.

Best Answer

This actually isn't that hard to do. I'll go down, page by page, what you would need to do to achieve this.

Articles Page

Create a new template in your theme folder (/wp-content/themes/your-theme/) called discusses.php and just add the following content:

<?php
/*
Template Name: Discusses Template
*/
?>

You don't need any more content. Go into the Pages section of WordPress Admin and create a new page called Articles page and set the URL to be /discusses/. Set the template of this page to be Discusses Template. Now, go into the Settings → Reading section in Admin and set the Posts page to Articles page. Now all your blog posts will appear under the URL /discusses/

Specific Article

The easiest way to have all your articles appearing as /discussed/title is to rename the default category (in Posts → Categories) to discussed. Then set your permalinks to be:

/%category%/%postname%/

This will render the links you need for each post.

Portfolio Page & Specific Design Page

Assuming this will be some kind of custom design, the easiest thing to do would be to create a custom template and Page in the pages section, as you did for the articles page, and throw your custom HTML and CSS into that. I would then set each specific design page to be a child page of that in the pages section. You can use the WPdb classs to make custom queries to populate the Portfolio page.

Related Topic