LuckyWP Table of Contents Shortcode

Below, you’ll find a detailed guide on how to add the LuckyWP Table of Contents Shortcode to your WordPress website, including its parameters, examples, and PHP function code. Additionally, we’ll assist you with common issues that might cause the LuckyWP Table of Contents Plugin shortcode not to show or not to work correctly.

Before starting, here is an overview of the LuckyWP Table of Contents Plugin and the shortcodes it provides:

Plugin Icon
LuckyWP Table of Contents

"LuckyWP Table of Contents is a powerful WordPress plugin that allows you to create organized, user-friendly content with dynamic, easy-to-navigate tables for improved readability."

★★★★☆ (797) Active Installs: 100000+ Tested with: 5.9.8 PHP Version: 5.6.20
Included Shortcodes:
  • [luckywp_table_of_contents]

LuckyWP Table of Contents [luckywp_table_of_contents] Shortcode

The LuckyWP Table of Contents shortcode is a powerful tool that generates a table of contents for your WordPress post. It dynamically fetches headings from your post and presents them in a structured manner. It checks if the plugin is activated and if the content is allowed. If it is, it processes the post’s settings and attributes. This includes settings like ‘min’, ‘depth’, ‘hierarchical’, ‘numeration’, and more. The shortcode then generates the table of contents based on these settings. It also handles skipping certain heading levels and texts as per the plugin’s settings. In essence, it enhances navigability and structure in your post.

Shortcode: [luckywp_table_of_contents]

Parameters

Here is a list of all possible luckywp_table_of_contents shortcode parameters and attributes:

  • min – Minimum number of headings to show the table
  • depth – Depth of the table of contents
  • hierarchical – Determines if the table is hierarchical
  • numeration – Type of numeration for the table
  • numerationSuffix – Suffix for numeration
  • title – Title of the table of contents
  • toggle – Option to toggle visibility of the table
  • labelShow – Label for the ‘show’ button
  • labelHide – Label for the ‘hide’ button
  • hideItems – Option to hide items initially
  • smoothScroll – Enables smooth scrolling
  • smoothScrollOffset – Offset for smooth scrolling
  • width – Width of the table of contents
  • float – Floats the table to left, right or none
  • titleFontSize – Font size of the title
  • titleFontWeight – Font weight of the title
  • itemsFontSize – Font size of the items
  • colorScheme – Color scheme of the table
  • backgroundColor – Background color of the table
  • borderColor – Border color of the table
  • titleColor – Color of the title
  • linkColor – Color of the links
  • hoverLinkColor – Color of the links on hover
  • visitedLinkColor – Color of visited links
  • wrapNoindex – Wraps the table in noindex tags
  • useNofollow – Adds nofollow to links
  • skipHeadingLevel – Skips a specific heading level
  • skipHeadingText – Skips headings with specific text
  • containerClass – Adds a specific class to the table container

Examples and Usage

Basic example – Displays a table of content for a post or a page using its ID.

[luckywp_table_of_contents id=1 /]

Advanced examples

Display a table of content with a specific depth and hierarchical structure. The ‘min’ attribute sets the minimum number of headings to display the table of contents, ‘depth’ attribute sets the maximum level of headings to include in the table of contents, and ‘hierarchical’ attribute enables or disables hierarchical view.

[luckywp_table_of_contents id=1 min=3 depth=6 hierarchical=true /]

Display a table of content with customized title and numeration. The ‘title’ attribute sets the title of the table of contents, ‘numeration’ attribute sets the type of numeration, and ‘numerationSuffix’ attribute sets the character or string after the numeration.

[luckywp_table_of_contents id=1 title="Table of Contents" numeration="decimal" numerationSuffix="." /]

Display a table of content with custom colors. The ‘backgroundColor’ attribute sets the background color of the table of contents, ‘borderColor’ attribute sets the border color, and ‘titleColor’, ‘linkColor’, ‘hoverLinkColor’, ‘visitedLinkColor’ attributes set the color of the title, links, hovered links, and visited links respectively.

[luckywp_table_of_contents id=1 backgroundColor="#f5f5f5" borderColor="#ddd" titleColor="#333" linkColor="#007bff" hoverLinkColor="#0056b3" visitedLinkColor="#6c757d" /]

PHP Function Code

In case you have difficulties debugging what causing issues with [luckywp_table_of_contents] shortcode, check below the related PHP functions code.

Shortcode line:

add_shortcode($this->getTag(), [$this, 'shortcode']);

Shortcode PHP function:

function shortcode($attrs)
    {
        global $post;
        if ($this->isDiactivated() ||
            ($this->theContentApplied && $this->headingsCache === null) ||
            !static::allow()
        ) {
            return '';
        }

        $attrs = ValueHelper::assertArray($attrs);
        if ($post instanceof WP_Post) {
            $postSettings = new PostSettings($post->ID);
            if ($postSettings->enabled || $postSettings->processHeadings) {
                foreach ([
                             'min',
                             'depth',
                             'hierarchical',
                             'numeration',
                             'numerationSuffix',
                             'title',
                             'toggle',
                             'labelShow',
                             'labelHide',
                             'hideItems',
                             'smoothScroll',
                             'smoothScrollOffset',
                             'width',
                             'float',
                             'titleFontSize',
                             'titleFontWeight',
                             'itemsFontSize',
                             'colorScheme',
                             'backgroundColor',
                             'borderColor',
                             'titleColor',
                             'linkColor',
                             'hoverLinkColor',
                             'visitedLinkColor',
                             'wrapNoindex',
                             'useNofollow',
                             'skipHeadingLevel',
                             'skipHeadingText',
                             'containerClass',
                         ] as $var) {
                    if (!array_key_exists(strtolower($var), $attrs) &&
                        $postSettings->$var !== null
                    ) {
                        $attrs[strtolower($var)] = $postSettings->$var;
                    }
                }
            }
        }

        if (Core::$plugin->isTheContent) {
            return $this->make($attrs, true);
        }

        if (!$this->theContentApplied) {
            $this->skipLevels[] = Core::$plugin->skipHeadingLevelToArray(array_key_exists('skipheadinglevel', $attrs) ? $attrs['skipheadinglevel'] : Core::$plugin->settings->getMiscSkipHeadingLevel());
            $this->skipText[] = Core::$plugin->skipHeadingTextToArray(array_key_exists('skipheadingtext', $attrs) ? $attrs['skipheadingtext'] : Core::$plugin->settings->getMiscSkipHeadingText());
        }

        if ($this->headingsCache === null) {
            $dto = $this->makeContentHandlingDto();
            $dto->modify = false;

            $this->isTheContentEmulate = true;
            $dto->content = apply_filters('the_content', $post->post_content);
            $this->isTheContentEmulate = false;

            $this->headingsCache = ContentHandling::go($dto)->headings;
        }

        return Toc::render($this->headingsCache, $attrs);
    }

Code file location:

luckywp-table-of-contents/luckywp-table-of-contents/plugin/Shortcode.php

Conclusion

Now that you’ve learned how to embed the LuckyWP Table of Contents Plugin shortcode, understood the parameters, and seen code examples, it’s easy to use and debug any issue that might cause it to ‘not work’. If you still have difficulties with it, don’t hesitate to leave a comment below.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *