Displaying code snippets from WordPress custom fields
I'm trying to display code snippets entered into my custom field. An example code snippet entered into custom field - snippet-1
<?php
if (($wp_query->current_post + 1) < ($wp_query->post_count)) {
echo '<div class="post-item-divider">Post Divider</div>';
}
?>
If I try to display this code wrapped in <pre></pre>
tags in my page template like
<?php if ( get_post_meta($post->ID, 'snippet-1', true) ) : ?>
<pre><?php echo get_post_meta($post->ID, 'snippet-1', true) ?></pre>
<?php endif; ?>
but it returns nothing to the template. I understand WordPress is filtering the snippet out as it sees as PHP code to execute. Is there a way just to print this out on 开发者_高级运维the page as a code snippet?
Many thanks in advance
rob
Use htmlspecialchars()
to escape your code.
Update
echo htmlspecialchars(get_post_meta($post->ID, 'snippet-1', true));
精彩评论