开发者

Displaying code snippets from WordPress custom fields

开发者 https://www.devze.com 2023-01-15 15:07 出处:网络
I\'m trying to display code snippets entered into my custom field. An example code snippet entered into custom field - snippet-1

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));
0

精彩评论

暂无评论...
验证码 换一张
取 消