Query page: preserve page formating

dose anyone know how to get this query (or a different query) to preserve the page formatting?
Thanks

<?php
$my_postid = 663;//This is page id or post id
$content_post = get_post($my_postid);
$content = $content_post->post_content;
$content = apply_filters('the_content', $content);
$content = str_replace(']]>', ']]>', $content);
echo $content;
?>

EDIT

I did not find a way to alter the above Query to respect the original page formatting
but I did find a different Query that dose.

Notice the

<div id="my-id" class="format_text"> 

in the following query.

The “class=”format_text”” formats the query output to that of the original page formatting.

<?php 
// The Query
$the_query =new WP_Query('pagename=my-page-name');
// The Loop
while ( $the_query->have_posts() ) : $the_query->the_post();

?>
<div id="my-id" class="format_text">
    <h2 class="page-title"><?php the_title(); ?></h2>
    <?php the_content(); endwhile; ?>
</div>
<?php

// Reset Post Data
wp_reset_postdata();
?> 

Answer

You’re comparing apples to oranges here. Both queries do the exact same thing, but are presented through different code.

Your query:

<?php
$my_postid = 663;//This is page id or post id
$content_post = get_post($my_postid);
$content = $content_post->post_content;
$content = apply_filters('the_content', $content);
$content = str_replace(']]>', ']]>', $content);
echo $content;
?>

Merely pulls some content out of the database and dumps it directly to the screen. This other query example you presented:

<?php 
// The Query
$the_query =new WP_Query('pagename=my-page-name');
// The Loop
while ( $the_query->have_posts() ) : $the_query->the_post();

?>
<div id="my-id" class="format_text">
    <h2 class="page-title"><?php the_title(); ?></h2>
    <?php the_content(); endwhile; ?>
</div>
<?php

// Reset Post Data
wp_reset_postdata();
?> 

Will also pull some content out of the database, but it runs it through the standard WordPress loop and adds styling to the content. It doesn’t “preserve” the styling.

What I mean

Your code snippet takes some content, passes it through the the_content filter, and prints the result to the screen.

...
$content = apply_filters( 'the_content', $content );
...
echo $content;

The other code snippet does the exact same thing. The the_content() function is a wrapper for a bunch of code that gets the content from the database, passes it through the the_content filter, and prints the result to the screen.

...
$content = apply_filters( 'the_content', $content );
...
echo $content;

The difference between their execution and yours is in the use of standard loop functionality. Here’s how I would rewrite your code to use the same kind of loop functions and templates in the other code:

<?php
$my_postid = 663;
$content_post = get_post( $my_postid );
setup_postdata( $content_post );        // Set up necessary $post globals
?>

<div id="my-id" class="format_text">
    <h2 class="page-title"><?php the_title(); ?></h2>
    <?php the_content(); ?>
</div>

<?php
wp_reset_postdata();                    // Reset $post globals to what they were before

The call to setup_postdata() will populate the global $post variable with your selected post’s information. Both the_title() and the_content() reference that global variable when they extract information, run it through filters, and print data to the screen.

The call to wp_reset_postdata() will reset the global $post variable to whatever it was set for the current request before we started messing with it.

Typically, you’d only call the_title() and the_content() from within the loop, but if you set the global $post variable ahead of time, you can still make them work.

Attribution
Source : Link , Question Author : xyz , Answer Author : Community

Leave a Comment