【问题标题】:Using WordPress shortcodes when <?php echo getPageContent(); ?> is used to display content<?php echo getPageContent(); 时使用 WordPress 简码?> 用于显示内容
【发布时间】:2011-05-26 20:14:40
【问题描述】:

我正在开发一个单页网站,我在该单页上显示来自其他页面的内容。为此,我添加了一个允许我使用&lt;?php echo getPageContent(ID); ?&gt; 的函数,它工作正常,除非我需要显示短代码中的内容,它只是将代码作为文本返回。有什么变通办法吗?

【问题讨论】:

    标签: php wordpress echo shortcode


    【解决方案1】:

    要获得正确的格式并替换短代码,您需要应用挂钩到 the_content 标记的过滤器,如下所示:

    echo apply_filters('the_content', getPageContent(ID));
    

    【讨论】:

      【解决方案2】:

      您选择这种策略来显示内容有什么原因吗?使用更符合正常 wordpress 页面开发和模板系统的东西可能会解决您的问题。我建议使用 get_posts() 和 setup_postdata() 的组合

      来自 WordPress 的文档:

      <?php
      global $post;
      $tmp_post = $post;
      $args = array( 'numberposts' => 5, 'offset'=> 1, 'category' => 1 );
      $myposts = get_posts( $args );
      foreach( $myposts as $post ) : setup_postdata($post); ?>
          <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
      <?php endforeach; ?>
      <?php $post = $tmp_post;
      ?>
      

      见:http://codex.wordpress.org/Template_Tags/get_posts

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-08-21
        • 1970-01-01
        • 2019-06-20
        • 1970-01-01
        • 1970-01-01
        • 2014-02-27
        • 2021-07-30
        • 1970-01-01
        相关资源
        最近更新 更多