splorp / tersus

An achingly simple WordPress theme without all the usual cruft.
GNU General Public License v3.0
99 stars 6 forks source link

Handling Nonexistent Parent Directories #27

Open splorp opened 12 years ago

splorp commented 12 years ago

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 commented 12 years ago

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 commented 12 years ago

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 commented 12 years ago

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 commented 12 years ago

Short answer, yes.

cdharrison commented 12 years ago

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 commented 12 years ago

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 commented 12 years ago

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

splorp commented 12 years ago

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 commented 12 years ago

Just committed fixes for this.

Preview links:

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

splorp commented 12 years ago

Rawk.

splorp commented 12 years ago

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 commented 12 years ago

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

cdharrison commented 12 years ago

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 commented 12 years ago

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

splorp commented 12 years ago

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 commented 12 years ago

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 commented 12 years ago

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 commented 12 years ago

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 commented 12 years ago

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 commented 12 years ago

$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 commented 12 years ago

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 commented 12 years ago

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 commented 12 years ago

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 commented 12 years ago

The likelihood is very high. :/

splorp commented 12 years ago

Confirmed.

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

splorp commented 12 years ago

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 commented 12 years ago

I love you man.