【问题标题】:Wordpress shortcodes only working in posts, not pages. Custom themeWordpress 短代码仅适用于帖子,而不适用于页面。自定义主题
【发布时间】:2014-01-01 07:39:07
【问题描述】:

我正在为朋友构建一个主题,但由于某些原因,我无法在 Pages 中使用简码。他们只在帖子中工作。

我的page.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]”。

可能是什么问题?

【问题讨论】:

    标签: php wordpress wordpress-theming


    【解决方案1】:

    您的帖子内容通过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;
    ?>
    

    【讨论】:

    • 完美。谢谢你的帮助,Ennui!
    • @user1636130 有机会时标记为解决方案!我也刚刚用使用the_title()the_content() 而不是get_the_title()get_the_content() 的固定代码更新了它。
    • 感谢您的宝贵时间 :) 我会尽快标记它。
    • 您也可以使用$content = get_the_content(); echo apply_filters('the_content', $content);,它将get_the_content() 返回的未过滤内容通过the_content() 上使用的默认过滤器。
    猜你喜欢
    • 2018-06-16
    • 1970-01-01
    • 2023-03-31
    • 1970-01-01
    • 1970-01-01
    • 2014-10-15
    • 1970-01-01
    • 2018-08-11
    • 1970-01-01
    相关资源
    最近更新 更多