• Hi all,

    Currently I’m working on a site that has quite some content on the archive and category pages. Is there any way to display the h2’s and h3’s (as TOC) with a link to them in WordPress?

    So I want to show a Table of content (based on the h2’s and h3’s) on the category and archive pages.

    Would love to hear from you!

Viewing 5 replies - 1 through 5 (of 5 total)
  • Is your theme showing full content on the archive pages?
    If so, I would change it to excerpts so that there is no duplicate content penalty from the search engines.
    There are plugins that generate Table of Contents from headings, although they usually only are for single pages, not archives.
    You can make that sort of function for use as an excerpt generator though, so the archive page shows excerpts that consist of the headings.

    Thread Starter Klaas Koopman

    (@inspired-media)

    Hi Joy,

    Thank you for your response.

    My theme is not showing full content on the archive page (you mean full content of the posts in that archive I guess?).

    “You can make that sort of function for use as an excerpt generator though, so the archive page shows excerpts that consist of the headings.”
    But how do I do this? That is the question!

    Thank you again for your reply and patience with me.

    So your theme is already showing excerpts, but they are too long? You can filter the excerpt length to make it shorter. ??

    Since you said you wanted a TOC, you can write a function similar to the core function that generates excerpts ( https://developer.www.ads-software.com/reference/functions/wp_trim_excerpt/ ), but instead make it generate a TOC. Then add that function as a filter before the core function runs: (the core on is at priority 10)
    add_filter( 'get_the_excerpt', 'mycustom_trim_excerpt', 9, 2 );

    The details of the function can be found in a TOC plugin.

    Hello we’ve got a similiar problem.

    As we changed our Shop to an Affiliate Site and using the product-categories from woocommerce as LP’s we have no idea how to implement a toc into it. No toc plugin works on archives so far. Any idea how to do it without doing it manually ?

    Example: https://www.hundekochprofi.de/produkt-kategorie/nahrungsergaenzungsmittel/

    Best max

    I was messing around with this today, and thought I’d post it here. But it does not contain links, just headings in a nested list.

    /**
     * Substitute a Table of Contents for the excerpt if there are more than 2 headings.
     */
    function my_excerpt_TOC ( $text, $num_words, $more, $original_text ) {
    	$many = preg_match_all( "/(<(h[\w]+)[^>]*>)(.*)(<\/\\2>)/", $original_text, $matches, PREG_SET_ORDER );
    	$out = '';
    	if ( $many > 2 ) {
    		foreach ($matches as $val) {
    			$level = intval( ltrim( $val[2], 'h' ) );
    			if ( isset( $save ) ) {
    				$out .= ( $save > $level ) ? '</li>' . str_repeat( '</ul></li>', $save-$level ) 
    					: ( ( $save < $level ) ? str_repeat( '<ul>', $level-$save ) : '</li>' );
    			}
    			else $first = $level;
    			$save = $level;
    			$out .=  '<li>' . $val[3];
    		}
    		return '<ul>' . $out . '</li>' . str_repeat( '</ul></li>', $level-$first ) . '</ul>';
    	}
    	return $text;
    }
    add_filter( 'wp_trim_words', 'my_excerpt_TOC', 11, 4 );
    
Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Table Of Content on Archive/Category Pages?’ is closed to new replies.