Archived topic
Changing the position of the table of contents
7 replies · Started by CRAIG on May 11, 2021
It would be useful for me to be able to change the position of the table of contents in my blog posts.
Using a code snippet found in another support topic on this site I have been able to find out how to move the featured image in a blog post.
https://generatepress.com/forums/topic/featured-image-inside-the-article/#post-708308
I would like to do the same thing with the table of contents. Specifically, I'd like to move the table of contents to directly below the featured image (which has now been moved using the snippet referred to above).
I am using the “Easy table of contents” plugin which has an option to change the position of the table, but it is limited to ‘before the first heading’, ‘after the first heading, ‘top’, and ‘bottom’, and none of these "standard" options do what I need it to.
Thank you for any help in advance.
Best,
Craig
Hi there,
so this part of the snippet:
add_filter( 'the_content', 'insert_featured_image', 20 );
function insert_featured_image( $content ) {
$feat_img = get_the_post_thumbnail($post->ID, 'post-single');
if ( is_single() && ! is_admin() ) {
return prefix_insert_after_paragraph( $feat_img, 2, $content );
}
return $content;
}
We can change to:
add_filter( 'the_content', 'insert_featured_image', 20 );
function insert_featured_image( $content ) {
$feat_img = get_the_post_thumbnail($post->ID, 'post-single');
$toc = do_shortcode( '[your-shortcode]' );
$insert = $feat_img . $toc;
if ( is_single() && ! is_admin() ) {
return prefix_insert_after_paragraph( $insert, 2, $content );
}
return $content;
}
Make sure to edit this line: $toc = do_shortcode( '[your-shortcode]' ); to include the ToC Shortcode.
I have edited the code as you have suggested but used "$toc = do_shortcode( '[ez-toc]' );"
This appears to kill the site apart from the header
I also tried [toc] as the shortcode but the same thing happens
I removed the snippet and the site goes back to normal again
This is the snippet I tried:
add_filter( 'the_content', 'insert_featured_image', 20 );
function insert_featured_image( $content ) {
$feat_img = get_the_post_thumbnail($post->ID, 'post-single');
$toc = do_shortcode( '[toc]' );
$html = $feat_img . $toc;
if ( is_single() && ! is_admin() ) {
return prefix_insert_after_paragraph( $html, 2, $content );
}
return $content;
}
// Parent Function that makes the magic happen
function prefix_insert_after_paragraph( $insertion, $paragraph_id, $content ) {
$closing_p = '</p>';
$paragraphs = explode( $closing_p, $content );
foreach ($paragraphs as $index => $paragraph) {
if ( trim( $paragraph ) ) {
$paragraphs[$index] .= $closing_p;
}
if ( $paragraph_id == $index + 1 ) {
$paragraphs[$index] .= $insertion;
}
}
return implode( '', $paragraphs );
}
I made a slight change to that updated code here:
https://generatepress.com/forums/topic/changing-the-position-of-the-table-of-contents/#post-1774089
This is the code I am now running:
add_filter( 'the_content', 'insert_featured_image', 20 );
function insert_featured_image( $content ) {
$feat_img = get_the_post_thumbnail($post->ID, 'post-single');
$toc = do_shortcode( 'toc' );
$insert = $feat_img . $toc;
if ( is_single() && ! is_admin() ) {
return prefix_insert_after_paragraph( $insert, 2, $content );
}
return $content;
}
// Parent Function that makes the magic happen
function prefix_insert_after_paragraph( $insertion, $paragraph_id, $content ) {
$closing_p = '</p>';
$paragraphs = explode( $closing_p, $content );
foreach ($paragraphs as $index => $paragraph) {
if ( trim( $paragraph ) ) {
$paragraphs[$index] .= $closing_p;
}
if ( $paragraph_id == $index + 1 ) {
$paragraphs[$index] .= $insertion;
}
}
return implode( '', $paragraphs );
}
I have put a link to the site as a private reply. You can see that the shortcode is displayed in the correct place but hasn't activated the table for some reason
This line:
$toc = do_shortcode('toc' );
needs to include the full shortcode as if you were adding to a page.
I think that plugin uses: [ez-toc]
So change that line to:
$toc = do_shortcode('[ez-toc]');
If nothing displays then i probably got the wrong shortcode name and you may need to check with developer.
Hi David,
I got the feeling that the plugin was causing the problem, just a gut feeling, and I couldn't get it confirmed regarding the correct shortcode.
Installed LuckyWP Table of Contents, added their shortcode [lwptoc] to the snippet you provided, and it works perfectly
Thank you very much for your assistance
Craig
Glad to be of help