Skip to content

Handling Nonexistent Parent Directories #27

Open
splorp opened this Issue Dec 11, 2011 · 27 comments

2 participants

@splorp
Owner
splorp commented Dec 11, 2011

I’ve been thinking about what we could do to automatically (and cleanly) handle parent directories that do not exist by default.

For example, the contents of a given category are available at a URL such as:

http://splorp.me/category/getoffmylawn

But there is no page designated for the parent directory:

http://splorp.me/category/

It would make sense to provide a “category” landing page of some sort, even if it simply echoes a restyled version of the wp_list_categories() function used in the sidebar.

Along with the /category/ subdirectory, we should handle /tag/ and /date/ as well.

@cdharrison cdharrison added a commit that referenced this issue Dec 22, 2011
@cdharrison cdharrison Added theme support for tag archives.
Related to Issue #27.
785ebb0
@cdharrison
Collaborator

Just committed a category.php and tag.php file to the theme that should address this. In trying to avoid adding classes to the h1 and category/tag description for these templates, I used some adjacent selectors in layout-advanced.css that may not work in IE6.

Live preview of these changes:

@cdharrison
Collaborator

Date-based archives are handled by archive.php. If what I've done for categories and tags works for you, I can update archive.php to match it better...

@cdharrison
Collaborator

Re-reading the issue, I think I misunderstood the request. If the parent category is empty, you want a list of children posted, correct?

@splorp
Owner
splorp commented Dec 22, 2011

Short answer, yes.

@cdharrison
Collaborator

Rough, but work in progress: http://cdharrison.com/stuff/design/ - list of child categories is displayed below content. (Placement can be modified, obviously.) Will need to look into this further but it looks like by default parent categories are never empty if their child categories have posts assigned to them.

Case in point:

http://cdharrison.com/stuff/quotes/

Has no posts directly assigned to it. The post displayed actually belongs to a child category:

http://cdharrison.com/stuff/quotes/faith

We could exclude children from the parent pages, but it would run counter to what WP does by default.

@splorp
Owner
splorp commented Dec 23, 2011

It’s a great start.

The list of child categories feels like it should only appear once on the page. Specifically, in relation to the parent category name and description at the top of the page.

@cdharrison
Collaborator

Just committed an update that should improve display considerably on Advanced. Check it out when you get a chance.

@splorp
Owner
splorp commented Dec 28, 2011

I’ve found a bug with the current category views.

Pagination is missing, so the page only displays the first ten posts in the category. I haven’t tested tag archives, as I don’t currently use tags on any of my posts.

@cdharrison
Collaborator
@splorp
Owner
splorp commented Dec 28, 2011

Rawk.

@splorp
Owner
splorp commented Dec 31, 2011

I’ve tidied up the look and feel to more closely match the other page views.

The only thing that’s still missing is a handler for the /category/ and /tag/ virtual subdirectories. They still 404 by default.

@cdharrison
Collaborator
@cdharrison
Collaborator

After a bit of research on this, the only way it would be able to do this is by creating a custom template and then assigning that template to a page with a slug name of category or tag. Going to try and ping a few WordPress devs for their thoughts on the matter.

@splorp
Owner
splorp commented Jan 5, 2012

Hrm. And where do custom rules get defined? In functions.php?

I have to admit that it’s all a bit over my phpn00b head at this point.

@cdharrison
Collaborator

Good question. Probed a bit deeper and another developer suggested the following:

You could check to see if the page exists and if not, create it (and setting the page template). I think that's a better solution than touching rewrite rules to make something other than a page show up there. A warning that get_page_by_path() lacks caching which most blogs don't have anyway, so make your exists check smart.

I'm a bit lost, too. Still pursuing.

@cdharrison
Collaborator

But yeah, I believe if we went the first route, they'd likely go in functions.php and likely need to be able to write to the .htaccess file of the site...

@splorp
Owner
splorp commented Jan 5, 2012

Here’s a thought.

Since hitting /category/ or /tag/ results in a 404, could we not add some logic to 404.php to display content based on the current URI?

@cdharrison
Collaborator

Check out http://cdharrison.com/category/ Latest commit uses 404.php to do what you suggested. Not having luck getting it to work with tag_base :(

@cdharrison
Collaborator

$category_base grabs whatever the user has set. Default is category/ but this worked for me when I changed mine to /topics

Will investigate $tag_base further. My issues may be cache-related.

@splorp
Owner
splorp commented Jan 5, 2012

WordPress might also be smart redirecting to a post that uses “tag” as part of the post slug.

Note how:

http://cdharrison.com/tag/

Redirects to:

http://cdharrison.com/2006/12/14/tag-im-it/

And:

http://cdharrison.com/tags/

Redirects to:

http://cdharrison.com/2007/09/05/tags-in-wordpress-23/

@cdharrison
Collaborator

I disabled all content that mentions ‘tags’, now http://cdharrison.com/tags/ works. Only problem is WordPress doesn’t seem to make any differentiation between tags and categories. (Not sure why.) Workaround would be to simply have both types of archives listed. Work for you?

This will likely break for /tags/ if I re-enable the posts that have the word in the title.

@splorp
Owner
splorp commented Jan 7, 2012

I wonder if if including ‘category’ at the beginning of a post title would break it as well?

I’ll have to test that.

@cdharrison
Collaborator

The likelihood is very high. :/

@splorp
Owner
splorp commented Jan 8, 2012

Confirmed.

If the post slug starts with ‘category’ then the request gets redirected to the post.

@splorp
Owner
splorp commented Jan 22, 2012

I was noodling around with this again today.

It seemed that every 404 was resulting in the category list and tag cloud appearing.

It turns out that unless you specifically set a custom category or tag base directory in the Permalink Settings, the globals return null. I’ve added some logic to check whether category_base or tag_base have been set and am now comparing them to $_SERVER['REQUEST_URI'] instead of dirname().

Now if you hit /category/ (or whatever your happen to have set as the base category directory), only the category list is displayed. Likewise, if you hit /tag/ (or the equivalent base directory), just the tag cloud is displayed.

Every other non-existing page request shows the standard 404 messaging.

@cdharrison
Collaborator
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.