0

I made a post template using meta box(custom field). If I put data into metabox, the template part (contents with h2, h3, p) shows up at the bottom of post (not on post edit page). However, every TOC plugins use "revisions" to check if there is h2 or h3 like this code.

public function widget( $args, $instance ) {
        global $ht_kb_toc_tools, $wp_query;

        if( ! is_singular() )
            return;

        if ( ! isset( $wp_query ) ) {
            return;
        }


        if( is_a($ht_kb_toc_tools, 'HT_KB_TOC_Tools') ){
            if( $ht_kb_toc_tools->ht_block_toc_detected && apply_filters('ht_kb_toc_disable_on_ht_block_toc', true ) ){
                //early exit if ht-block-toc detected
                return;    
            }                
        }

        extract( $args, EXTR_SKIP );

        $instance = wp_parse_args( $instance, $this->defaults );

        //$post = get_post( $wp_query->post->ID );
        $post = get_post();

        if( is_preview() ){
            //get the post revisions
            $post_revisions = ( wp_get_post_revisions( $post ) );

            if ( !empty( $post_revisions ) ) {
                //get the latest revision - this should be the current preview
                $post = current( $post_revisions );
            }
        }

        $title = apply_filters( 'widget_title', empty( $instance['title'] ) ? '' : $instance['title'], $instance, $this->id_base );   

        if(is_a($ht_kb_toc_tools, 'HT_KB_TOC_Tools')){

            //extract headings
            $headings = $ht_kb_toc_tools->ht_kb_toc_extract_headings( do_shortcode( $post->post_content ), true ); 

            //don't output widget if no headings are in content
            if(empty($headings))
                return;

            echo $before_widget;

            if ( $title )
                echo $before_title . $title . $after_title;


            ?>
            <nav id="navtoc" role="navigation">

                

            <?php
            //display items
            $ht_kb_toc_tools->ht_kb_display_items();
            ?>

            </nav>

            <?php
        }

        echo $after_widget;

    } // end widget

Is there another way to check h2 or h3 beside checking revision? or is there a way that shows my metabox data show on post edit page? Thanks.

1 Answer 1

0

Even if they're not looking at revisions, all table of contents plugins are likely to only look at post_content rather than postmeta, because postmeta can be anything and it would be hard to query for something that doesn't exist on every site.

Your best bet may be to update your site to use the Block Editor. From there, you can create a custom block that displays the postmeta within the post_content. Then, some plugins may be able to pick up the heading tags - though some may still not, so you might want to first experiment with some other blocks that create heading tags that are not the Core Heading block to find a TOC plugin that picks those up.

1
  • Thanks for the idea. I will try to find a way of doing it with custom block then. Thanks! Commented May 13, 2022 at 15:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.