Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wordpress shortcodes only working in posts, not pages. Custom theme

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?

like image 604
user1636130 Avatar asked Dec 13 '13 13:12

user1636130


1 Answers

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;
?>
like image 149
Ennui Avatar answered Sep 19 '22 01:09

Ennui