Вопрос:

How to format post content of WordPress in html format

html wordpress

1532 просмотра

2 ответа

375 Репутация автора

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.

Автор: sasuke Источник Размещён: 26.04.2017 11:21

Ответы (2)


2 плюса

659 Репутация автора

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.

Автор: Prateek Verma Размещён: 26.04.2017 11:50

0 плюса

766 Репутация автора

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();
?>
Автор: RBCunhaDesign Размещён: 26.04.2017 12:33
Вопросы из категории :
32x32