5

I'm building a theme for a friend but some some reason I can't get shortcodes to work in Pages. They only work in Posts.

My page.php file is very simple at the moment:

<?php get_header(); ?>
<?php
if (have_posts()) :
    while (have_posts()) : the_post();
        echo '<div class="hero-unit"><div class="container"><h1>'.get_the_title().'</h1></div></div>';
        echo '<div class="container clearfix" id="main-content">'.get_the_content().'</div>';
    endwhile;
endif;
?>
<?php get_footer(); ?>

This is working fine but is just displaying the shortcode as text. IE I am trying to use the shortcode [wp_sitemap_page] the page just renders '[wp_sitemap_page]' in text.

What could be the issue?

1 Answer 1

9

Your post content is displayed via echo get_the_content() which is a function that returns the content WITHOUT applying the default filters (wpautop, do_shortcode etc) that are applied normally when you use the_content() instead.

This should fix it:

<?php
if (have_posts()) :
    while (have_posts()) : the_post(); ?>
        <div class="hero-unit"><div class="container"><h1><?php the_title(); ?></h1></div></div>
        <div class="container clearfix" id="main-content"><?php the_content(); ?></div>
    <?php endwhile;
endif;
?>
3
  • @user1636130 Mark as the solution when you get a chance! I also just updated it with the fixed code that uses the_title() and the_content() rather than get_the_title() and get_the_content().
    – Ennui
    Dec 13, 2013 at 13:55
  • Thanks for your time :) I will mark it as soon as I'm allowed. Dec 13, 2013 at 13:57
  • 3
    You could also do $content = get_the_content(); echo apply_filters('the_content', $content); which will pass the unfiltered content returned by get_the_content() through the default filter used on the_content().
    – Ennui
    Dec 13, 2013 at 13:57

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.