2017-04-26 112 views
1

我的网站运行在WordPress服务器上,我使用WordPress数据库为我的自定义使用提取一些数据。我想知道如何将我的发布内容从WordPress的发布表中以格式化良好的html格式转换,就像在java或节点js中由WordPress rest api返回一样。如何格式化html格式的WordPress发布内容

+0

你能否提供你的代码,你如何显示你的内容,以便我可以以更好的方式帮助你。 –

回答

1

请使用apply_filters函数以格式良好的方式显示内容。

使用 -

<?php echo apply_filters('the_content', $post->post_content); ?> 

代替OF-

<?php echo $post->post_content; ?> 

希望,这可能会对你有所帮助。

+0

但我想要格式化另一种语言的内容(如nodeJs)并从wordpress数据库中提取数据。 – sasuke

0

编辑
这里是另一个post会回答你的问题。


我想你要找的是 WordPress Query Class。它的工作方式是创建一个要从数据库中提取的对象数组,然后通过它们循环显示在屏幕上。我正在检索仅包含没有内容的缩略图图像的帖子类型。

在此示例中,我创建了一个名为$args的数组以获取post_type => portfolio_item,我想显示30个项目posts-per-page => 30。然后,我将通过调用WP_Query的一个新实例来运行查询,调用参数并将其放在一个变量上。 $the_query = new WP_Query($args);

如果您的查询返回任何项目我想要获取项目,在这种情况下,一个帖子类型。 while ($the_query -> have_posts()) : $the_query -> the_post();。在循环内部,您可以以任何方式,形状或形式设置输出风格。

循环结束后不要忘记使用wp_reset_postdata();或它可以打破未来的查询类。

<?php 
    // Define our WP Query Parameters 
    $args = array(
     'post_type' => 'portfolio_item', 
     'posts-per-page' => 30, 
    ); 
    $the_query = new WP_Query($args); 

    // Start our WP Query 
    while ($the_query -> have_posts()) : $the_query -> the_post(); 

    // Display the Post Feature Image and Post Title ?> 
    <div <?php post_class('col-xs-12 col-sm-6 col-lg-4 mb-2 portfolio'); ?> data-cat="logo"> 
     <div class="portfolio-wrapper d-flex"> 
      <?php the_post_thumbnail('large', ['class' => 'align-self-center mx-auto jbox-img img-thumbnail']); ?> 
      <div class="label"> 
       <div class="label-text"> 
        <span class="text-category"><?php the_title() ?></span> 
       </div> 
       <div class="label-bg"></div> 
      </div> 
     </div> 
    </div> 
<?php 
    // Repeat the process and reset once it hits the limit 
    endwhile; 
    wp_reset_postdata(); 
?>