tersus icon indicating copy to clipboard operation
tersus copied to clipboard

Handling Nonexistent Parent Directories

Open splorp opened this issue 13 years ago • 27 comments

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.

splorp avatar Dec 11 '11 18:12 splorp

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:

  • http://cdharrison.com/stuff/personal/
  • http://cdharrison.com/tags/wls/

cdharrison avatar Dec 22 '11 18:12 cdharrison

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 avatar Dec 22 '11 18:12 cdharrison

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

cdharrison avatar Dec 22 '11 19:12 cdharrison

Short answer, yes.

splorp avatar Dec 22 '11 20:12 splorp

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.

cdharrison avatar Dec 22 '11 20:12 cdharrison

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.

splorp avatar Dec 23 '11 19:12 splorp

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

cdharrison avatar Dec 23 '11 20:12 cdharrison

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.

splorp avatar Dec 28 '11 17:12 splorp

Just committed fixes for this.

Preview links:

http://cdharrison.com/tags/photoshop/ http://cdharrison.com/stuff/design/webdevelopment/

cdharrison avatar Dec 28 '11 19:12 cdharrison

Rawk.

splorp avatar Dec 28 '11 21:12 splorp

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.

splorp avatar Dec 31 '11 20:12 splorp

Ah, now I understand. I'll see what I can do to fix.

cdharrison avatar Dec 31 '11 21:12 cdharrison

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.

cdharrison avatar Jan 03 '12 22:01 cdharrison

Here's the answer I got: http://twitter.com/#!/markjaquith/status/154324264047218688

cdharrison avatar Jan 03 '12 22:01 cdharrison

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.

splorp avatar Jan 05 '12 16:01 splorp

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 avatar Jan 05 '12 16:01 cdharrison

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...

cdharrison avatar Jan 05 '12 16:01 cdharrison

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?

splorp avatar Jan 05 '12 17:01 splorp

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 avatar Jan 05 '12 17:01 cdharrison

$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.

cdharrison avatar Jan 05 '12 17:01 cdharrison

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/

splorp avatar Jan 05 '12 19:01 splorp

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.

cdharrison avatar Jan 05 '12 19:01 cdharrison

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

I’ll have to test that.

splorp avatar Jan 07 '12 19:01 splorp

The likelihood is very high. :/

cdharrison avatar Jan 07 '12 19:01 cdharrison

Confirmed.

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

splorp avatar Jan 08 '12 17:01 splorp

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.

splorp avatar Jan 22 '12 22:01 splorp

I love you man.

cdharrison avatar Jan 22 '12 22:01 cdharrison