admin管理员组文章数量:1130349
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;
}
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;
}
本文标签: Get post content before rendering
版权声明:本文标题:Get post content before rendering 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://it.en369.cn/questions/1749161828a2325524.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。


发表评论