WordPress短代码仅适用于帖子,而不是页面.自定义主题


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

我正在为朋友构建一个主题,但由于某种原因,我无法在 Pages 中获得短代码。他们只在帖子中工作。

我的页面.php文件目前非常简单:

<?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(); ?>

这工作正常,但只是将简码显示为文本。IE 我正在尝试使用短代码 [wp_sitemap_page]页面只是在文本中呈现"[wp_sitemap_page]"。

可能是什么问题?

您的帖子内容通过echo get_the_content()显示,这是一个返回内容的函数,无需应用默认过滤器(wpautopdo_shortcode等(,当您使用the_content()时通常会应用默认过滤器。

这应该可以解决它:

<?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;
?>