DISQUS

Sorrowful Unfounded: WordPress Tutorial – Table of Contents in a Page

  • Christopher Clarke · 3 months ago
    TL;DR version:

    1. Copy page.php template.
    2. Rename copy table_of_contents.php
    3. Make the edit I suggest in the main article.
    4. Save template / upload
    5. Create New Page
    6. Fill in title and content
    7. Select the Table of Contents custom template under Attributes.
    8. Add a custom field named "toc_category_id" with the value being the ID of the category containing your chapters.
    9. Publish
    10. Enjoy your new table of contents page.
  • Ashley · 3 months ago
    It's not too complicated. My PHP knowledge is virtually nill nowadays (I haven't done much programming in two years or so), but I think I can get this going ;) I didn't even think of using a custom page template : D
  • Christopher Clarke · 3 months ago
    I'm glad. Let me know if you get it work, please.
  • Ashley · 3 months ago
    I'm sure it's something simple like, "oh, you've got an extra apostrophe" or something, but it seems to be displaying every post, instead of just the posts from category ID "4".

    http://heiwuya.net/serial-fiction/the-crow/tabl... Is the page in question, and I've got screenshots of all the applicable code (I hope)

    http://img59.imageshack.us/img59/3285/editcateg...
    http://img185.imageshack.us/img185/4083/editthe...
    http://img32.imageshack.us/img32/897/tableofcon...

    I also double-checked it against the WP Codex, and tried hard-coding the value for the category in there. It seems fine to me, and I tried copy & pasting the example from the Codex, to no avail.
  • Christopher Clarke · 2 months ago
    I'm sorry for the late reply. WordPress marked the message as spam due the number of links. Looking at your site, it appears you fixed it, but I'll go over a solution here.

    Try this (I haven't tried it, but I think it should work):

    http://php.pastebin.com/f53118d4e

    Good luck. If this works, I'll replace the code in the tutorial with it.
  • Christopher Clarke · 2 months ago
    My apologies, WordPress seems to have killed my comment.

    Code:
    http://php.pastebin.com/f9be2b8e
  • Ashley · 2 months ago
    My fix was to replace category() with category_name().

    Your new code results in a blank page (my server seems to have turned off PHP errors). But the initial problem seems to be my installation. It seems to think every post is in category 4 (even when navigating in the WP control panel).
  • Christopher Clarke · 2 months ago
    That's weird. I tried it just now and it worked as expected. I know pastebin tends to add #'s when you copy and paste sometimes, did you remove those?

    I'm not sure about this WordPress category issue but I believe I had a similar issue when making the sidebar widget table of contents. No matter what I set the category to it was always getting the wrong posts. The solution was the same as the solution I used in the new code - using the database table field names directly instead of relying on WordPress's template functions for the title and permalink.
  • Ashley · 2 months ago
    Fixed it, it ended up being a problem with the actual database itself. A wipe of the database and a re-installation solved the problem, and both the first and second code snippets work perfectly now.

    I have no idea what could have caused the database to screw itself up so royally, but that was the problem, not your code! Sorry for the mix-up, and thanks a lot for the help!
  • Daven · 1 month ago
    Okay, I finally got a plugin that allows me to assign categories to my pages, and while the plugin is working for POSTS, it's not working for pages. I have used the code displayed exactly the way it is shown here (with the only modification being that I replaced OL with UL for asthetics) and it's not working.

    Is there something I'm doing wrong? This is a site I'm transferring into Wordpress, so it can blow up and I won't be TOO mad...
  • Christopher Clarke · 1 month ago
    The plugin and table of contents described in this tutorial doesn't support pages.

    I haven't tested but I think replacing $toc = get_post... with $toc = get_pages('child_of='.$post->ID.'&sort_column=post_date&sort_order=ASC'); should do the trick. Of course, replace '.$post->ID.' with the ID of the page that is the parent of the chapters you want to list if it isn't the page that holds the parent (even so you might still need to replace it, I don't know).

    Good luck.
  • Daven · 1 month ago
    Sir, you saved my backside. Changing the code like you mention fixes it for pages, but you do have to get a plugin to allow you to assign categories to your pages. With those two (plugin + your hack) I have now got index pages for a specific sub category.

    Oh, the plugin I used so that others can use it is located here: http://www.megaupload.com/?d=CLBDY6U0

    I downloaded that, implemented it, created categories, assigned them to pages and then, with that code change: http://davensjournal.com/Updating/beginning-wicca

    I'm VERY happy now.