Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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();
?> 
share|improve this question
1  
what do you mean by "preserve the page formatting"? Can you please explain what you want to achieve including what you've tried so far? –  HungryCoder Jun 19 '12 at 3:22
    
line-height, paragraph spacing, font-size, font-color... –  Tim Jun 20 '12 at 12:51

2 Answers 2

up vote 1 down vote accepted

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.

share|improve this answer
    
Wow, that was beyond helpful. I can see just how ignorant I am with respect to Querys. Thanks!!!, for the query and the schooling. –  Tim Jun 20 '12 at 20:36

I believe your question is not very clear. You did not give enough effort to explain the problem resulting no one could come forward to help you.

As in the comment you have said you are trying to preserve line heights, spacing, font size etc, all these are related to css. There is no relation to these with the wp query functions. As you are worried about preserving the formatting, i guess you mean that something has changing the formatting. If so, css is that evil (for you). You can write new css that will override any existing css. If your class format_text has the css that is overriding your formats, you can remove that class form the tag.

You are also triggering the_content hook from your code. If that filter is changing your formatting, you can remove the following line:

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

however, please note that it may also remove other features that are implemented by functions those are triggered by the_content filter.

i've tried to give suggestions based on assumptions as the question is not clear. this may or may not help you. if anything does not help, please post a bit more clear detail about the problem and scenario.

good luck.

share|improve this answer
    
Yes, I wrote a unclear question, though I did not know that at the time, this comment helps me understand what was unclear and I appreciate it, thank you... really I mean it. Your original comment was so terse I could not make anything of it boldwhat do you mean by "preserve the page formatting", that seemed self-evident to me and so I did not know how to answer it. I see that it meant something and in my ignorance did not see that. So thank you again for the time you took to resond –  Tim Jun 20 '12 at 20:33

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.