$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>Get post content before rendering|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

Get post content before rendering

matteradmin7PV0评论

I am currently working on a shortcode function, where I need to access the content of a post, before its rendering.

If I do it like this

$content = get_the_content( get_the_ID() );

It gives me the already rendered content. How can I access the content before rendering?

What I'm trying to do is create a shortcode, that generates a table of content from specific headlines that are placed with shortcodes. For that reason, the content still needs to have the unrendered shortcodes in it.

As requested, here is the complete code, so you might get an idea of what I am trying to do.


function nimaji_shortcodes() {

    add_shortcode( 'nimaji_toc', 'generate_toc' );

}

add_action( 'wp_head', 'nimaji_shortcodes' );

function generate_toc() {

    $content = get_the_content( get_the_ID() );
    krumo($content);

    // Look for specific shortcode, that has information about headlines in it
    $pattern = '/\[x_custom_headline level="(.*?)"(.*?)\](.*?)\[\/x_custom_headline\]/';

    preg_match_all( $pattern, $content, $matches );

    $levels = $matches[1];
    $titles = $matches[3];

    $toc_meta = generate_toc_meta( $levels, $titles );

    $html = '<ol>';

    foreach ( $toc_meta as $key_2 => $level2 ) {

        foreach ( $level2 as $key_3 => $level3 ) {

            if ( $key_3 == 0 ) {
                $html .= "<li>$level3</li>";
            } else if ( $key_3 == 1 ) {
                $html .= "<ol>";
                $html .= "<li>$level3</li>";
            } else if ( $key_3 == sizeof( $level2 ) - 1 ) {
                $html .= "<li>$level3</li>";
                $html .= "</ol>";
            } else {
                $html .= "<li>$level3</li>";
            }
        }

    }

    $html .= '</ol>';

    return $html;

}

function generate_toc_meta( array $levels, array $title ) {

    $new_levels = array();

    $i = - 1;
    $j = 0;
    foreach ( $levels as $key => $level ) {

        if ( $level == 'h2' ) {

            $i ++;
            $j                      = 0;
            $new_levels[ $i ][ $j ] = $title[$key];

        } else if ( $level == 'h3' ) {

            $j ++;
            $new_levels[ $i ][ $j ] = $title[$key];

        } else {

            continue;

        }

    }

    return $new_levels;
}

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far