Table of Content
This Plugin parses the given content of your page / post and scans for for h*-Tags (such as h1, h2, h3,..). Afterwards it generates a navigation (Table of Content = TOC) and prints the upgraded content (every h*-tag has a top link ..),
Installation
Either
- use the Wordpress installation
or
- Download the .zip file and extract into your Wordpress Plugin directory (wp-content/plugins/)
Then
- Activate the Plugin
- Use the shortcode in your Page or Post via some content with h* tags
Frequently Asked Questions
My Table of Content looks like Sauerkraut. Why ?
It is mandatory that your content has “correct” structure .. as in h1 -> h2 -> h3 and NOT in h2 -> h1 -> h3. You go crazy with your sturcture – so will this plugin.
I want to use this plugin in my template. How ?
This is easy.
$result = $TableOfContent->parse_contents( $contents, $args );
echo $result->navigation;
echo $result->content;
Download
Example
Here is the Syntax of the parsed Content
This is before the parsed content [ table-of-content ] These are the contents .. <h1>This is Toplevel</h1> something under toplevel <h2>This is second level</h2> something under second level <h3>This is third level</h3> something under third level <h3>Again third level</h3> even more under third <h2>Back again second</h2> here we go again second <h3>Down again third</h3> anothertime third <h1>And back to top</h1> top is back again [ /table-of-content ] This is after the parsed content
This is before the parsed content
And here is the rendered output
These are the contents ..
This is Toplevel^
something under toplevel
This is second level^
something under second level
This is third level^
something under third level
Again third level^
even more under third
Back again second^
here we go again second
Down again third^
anothertime third
And back to top^
top is back again
This is after the parsed content
When I tried this plugin it messed up all my image captions. It was just showing the caption shortcode and not the processed captions. After much searching I finally found if I added the following two lines before the “return parsed” in the plugin.php code it worked:
add_filter($parsed, ‘do_shortcode’, 11);
$parsed = apply_filters(‘the_content’, $parsed );
Thanks for the plugin!
OK, I’ve updated the plugin. Thanks for reporting!
Just tried to install this plugin (exactly what I’m looking for!) on WP 2.8.4 running on MediaTemple (gs). The plugin couldn’t be activated, due to the following error:
Parse error: syntax error, unexpected T_STRING, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or ‘}’ in /[REDACTED]/html/wp-content/plugins/table-of-content/includes.php on line 6
Right you are. The last patch didnt work out, now i have removed it.. try again
I have just been trying to install the Table of Contents plugin into Wordpress 2.8.4 but unfortunately when I try to activate the plugin I receive an error message;
………
Fatal error: Class ‘TableOfContent’ not found in C:\xampp\htdocs\wpplugin\wp-content\plugins\table-of-content\plugin.php on line 20
This is not the full code only the last section but i can print the full message if required.
Thanking you for your time and consideration.
Try to call the includes.php directly and see what happens (eg: http://blog.foaa.de/wp-content/plugins/scaleable-contact-form/includes.php). You should see a blank screen and no errors. If so, your includes path is wrong. Or maybe some PHP4 issue ?! Which version of PHP do you run ?
First, thank you for your quick reply.
I have tried calling the “includes.php” file as you suggested and get this;
# content; // replacement for “the_content()” * print $site->navigation; // the actual page navigation */ public function parse_contents( $post_content = “”, $args = array() ) { // using the whole content if empty content is specified if ( $post_content == “” ) $post_content = get_the_content(); // inti variables for this round .. $this->counter = 0; $this->matches = array(); $this->args = array_merge( array( ‘list_type’ => ‘ol’, ‘prefix’ => ”, ’suffix’ => ‘^’, ‘clear_iterator’ => true, ‘top_suffix’ => ”, ‘top_prefix’ => ”, ), $args ); // replace the suffix in the top-links $this->args[ 'prefix' ] = preg_replace( ‘/%prefix%/’, $this->args[ 'top_prefix' ], $this->args[ 'prefix' ] ); $this->args[ 'suffix' ] = preg_replace( ‘/%suffix%/’, $this->args[ 'top_suffix' ], $this->args[ 'suffix' ] ); // generate some random number for the being unique $rand = $this->args[ 'rand' ] = (int)( rand() * 99999999 ); // update content $post_content = preg_replace_callback( ‘/(.+?)/s’, array( &$this, ‘_replace_and_build_navi’ ), $post_content ); // having matches, build the navi $navigation = ”; if ( !empty( $this->matches ) ) { $start_level = 0; $last_level = 0; $navigation = ‘args[ 'list_type' ]. ‘>’; $i = 0; foreach( $this->matches as $match ) { $level = $match[0]; $title = $match[1]; // first level if ( !$start_level ) $start_level = $level; // same level elseif ( $last_level == $level ) $navigation .= ”; // level UP .. elseif ( $last_level 0 ) $navigation .= ‘args[ 'list_type' ]. ‘>’; } // level DOWN elseif ( $last_level > $level ) { $diff = $last_level – $level; while ( $diff– > 0 ) $navigation .= ‘args[ 'list_type' ]. ‘>’; } // remember last level .. $last_level = $level; // insert current level $navigation .= ”. $title. ”; $i++; } // append finisher $diff = $last_level – $start_level; $diff ++; while ( $diff– > 0 ) $navigation .= ‘
args[ 'list_type' ]. ‘>’; } return (object)array( ‘content’ => $post_content, ‘navigation’ => $navigation ); } // replace method for all h-tags .. private function _replace_and_build_navi( $match ) { $level = $match[1]; $title = $match[2]; // remove leading “123.” .. if ( $this->args[ 'clear_iterator' ] ) $title = preg_replace( ‘/^\s*\d+\.?\s*/’, ”, $title ); // add to matches $this->matches []= array( $level, strip_tags( $title ) ); // rebuild and return the h*-tag return join( ”, array( ”, $this->args[ 'prefix' ], ”, $title, $this->args[ 'suffix' ], ” ) ); } } ?>
For your information I am using Xampp (1.7.2) as a local testing server which runs on PHP 5.3.0. I am presently testing before uploading to my web server which runs on PHP 4.4.9.
@Paul: Huh, it looks like your webserver doesnt interpret the .php-file at all – its a content dump. This seems to be the source of your previous problem. I’m not at all familiar with Windows so cant help with your setup..
Just to say thank you for your quick reply once again.
I should be uploading to my web server at the weekend, having nearly finished my theme, and this runs on Linux so I will give it a try then.
Once again thank you for your time and consideration
Yours Paul
Hi,
Looks interesting and good to see a nice clean take on TOC generators.
I do have one feature request:
instead of having to wrap the text in [table-of-content] tags, would it be possible to just put one tag at the top of the post and then all the post text is put in a TOC? (e.g [TOC] makes one for all the posts content)
Thanks,
Ben.
This works well, but I don’t want the back to top^ links automatically inserted.
1) Can you make it optional in future releases,
2 )Tell me how to remove it now?
@Shoq: You can easily hide them via CSS: .toplink { display: none }.
Yeah, Thanks. New to WP, and that dawned on me as soon as I went to lunch. I guess I can just add a background image, too. I don’t mind the link. Just the styling of it.
Wordpress 2.9 when I try to activate the plugin:
Parse error: syntax error, unexpected T_STRING, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or ‘}’ in /url-top-plugin-folder/table-of-content/includes.php on line 6
@UK I am new to css. where in the css file would I locate the ” toplink { display: none }” code to get rid of the ^ back to top links
@mooreway: Just add it to the css-file of your theme (probably called styles.css in your theme folder..)
@Kenneth: Hmm, i’ve updated to 2.9.1 too, and it seems to work (see this page). However, i’ve re-uploaded the current release to wordpresse. Try the new relase 0.4.1 again (download from here or wait for WP Plugin Repository) and tell me wheter it works or not.
Thanks UK that worked out..