Warning: fairly pedantic request ahead.
I have a design that makes use of posts (I.e., the blog) and a custom post type called “howto”. The site front page (“/”) has a template that displays the latest blog posts and latest howtos. When the user navigates to the blog section (“/blog”), they see a different template than when they navigate to the howto section (“/howto”).
I know the obvious way of doing this is to create two pages (“Blog” and “Home”), then use the drop-downs in Settings->Reading Options to explicitly set those. I can then associate specific page templates to those posts.
But darned if that isn’t really messy — I’ll have two useless empty pages cluttering any admin-side list of pages with actual content in them. You’d think there’d be some way to use template hinting to prevent this from being necessary:
- /howto/ -> archive-howto.php
- /blog/ -> archive.php
So really, I guess the question really is as follows:
How does one set a custom path for posts without creating a new page?
I don’t have time to explain this in detail (I shall upon return) but in the meantime this should work for you,Answer updated with explanation as promised.
WP Rewrite rules are like voodoo, I'm sure there's more than one way to go about this, but here's mine.
Problem:
Just to clarify your question for others who may stumble upon this thread, what you want to do is create a page without having to physically create an empty-placeholder-page within the administration dashboard found under:
Page -> Add New
.Essentially, you want to create a Fake Page and have that page use any template you specify.
Solution:
First we setup our rewrite rules,
Within the
add_rewrite_tag
we specify our query variable in the form of %fake_page%, which by the way you can specify whatever you want or appropriate for your needs. My examplefake_page
is only symbolic to illustrate the mechanics of this answer.How the query variable works in this instance is by matching a request for,
…which is then internally mapped to,
The latter being what you would see when running the default permalink structure.
In a similar fashion requests for,
…would each map to their equivalents,
In the example snippet above you will notice that we have the rewrite rule which firsts matches the pagination
/blog/page/{page_number}
, above our second rule which matches the base for our fake page of/blog
.This is necessary so that the base rule doesn’t return a match on the first occurrence of our endpoint which is defined as
blog
before having a chance to evaluate the rest of the requested URL to ensure that the user hadn’t in fact requested a paged result. Basically, reverse those rules and it doesn’t work.The next function which hooks onto
template_redirect
checks for the existence of ourfake_page
within the query variables, if its matched/exists within the array we then request the inclusion of our desired template file to handle the presentation of data.PS. I have tested this and it works under my conditions, though I'm not sure if any other quirks may popup with rewrite rules and end point masks the deeper you go, therefore you should thoroughly test all pagination and permalinks and make sure that they resolve to their intended paths correctly . If not, we can address those issues as they arise.
Make two archive pages.
archive.php for your blog posts, and archive-howto.php for your howtos.
http://codex.wordpress.org/images/1/18/Template_Hierarchy.png
Making fake pages and futzing with the rewrites is way overkill and somewhat dangerous.
Unless of course I’ve completely missed the point.
Maybe you are looking for this:
These posts will appear in the WP admin with
post_type=my_howtos
, while on the public facing side the URL-s will appear as/howtos/
, and the filearchive-my_howtos.php
in the theme will be used to display the page.The
HOWTO_SLUG
andHOWTO_REWRITE_SLUG
can’t be the same, or it will break.