baldraider
baldraider

Reputation: 1069

How to format post content of WordPress in html format

I have my website running on WordPress server and I'm using WordPress database to extract some data for my custom use. I want to know how to convert my post content from post table of WordPress in well formatted html format just like as one return by WordPress rest api in java or node js.

Upvotes: 2

Views: 4418

Answers (2)

Raphael Cunha
Raphael Cunha

Reputation: 1114

EDIT
Here is another post that will answer your question.


I think what you're looking for is WordPress Query Class. The way it works is you create an array of objects you want to pull from the database and then loop through them to display on the screen. I am retrieving a post type that only has a thumbnail image with no content.

On this example here I created an array called $args to get post_type => portfolio_item and I want to display 30 items posts-per-page => 30. I will then run the query by calling a new instance of WP_Query, call the arguments and place it on a variable. $the_query = new WP_Query( $args );.

If your query returns any items I want to get the items, in this case a post type. while ( $the_query -> have_posts() ) : $the_query -> the_post();. Inside the loop you can style the output in any way, shape, or form.

After the loop is over don't forget to use the wp_reset_postdata(); or it can break future Query classes.

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

Upvotes: 0

Prateek Verma
Prateek Verma

Reputation: 889

Please use apply_filters function for displaying the content in a well formatted way.

Use-

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

instead of-

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

Hope, this may be helpful to you.

Upvotes: 3

Related Questions