Reputation: 9147
I know this is really simple but it just isn't coming to me for some reason and google isn't helping me today.
I want to output the pages content, how do I do that?
I thought it was this:
<?php echo the_content(); ?>
Upvotes: 44
Views: 221016
Reputation: 796
All the answers seem to do it on a page basis which feels rather quick and dirty. You might want to reuse this so why not just add a simple page template?
Save the following content to a PHP file, e.g. cleanpage.php
and upload it to your wp-content/themes/WhatEverThemeYouAreUsing
directory.
I just opened nano
on my webserver in the directory mentioned above and copied everything in there.
<?php
/**
* Template Name: Clean Page
* This template will only display the content you entered in the page editor
*/
?>
<!DOCTYPE html>
<html <?php language_attributes(); ?>>
<head>
<meta charset="<?php bloginfo( 'charset' ); ?>">
<meta name="viewport" content="width=device-width, initial-scale=1">
<?php wp_head(); ?>
</head>
<body class="cleanpage">
<?php
while ( have_posts() ) : the_post();
the_content();
endwhile;
?>
<?php wp_footer(); ?>
</body>
</html>
Then you can just select it on your page as a template like this:
Upvotes: -1
Reputation: 1079
"The Loop" is bad practice. WordPress should have never implemented it, and it should be deprecated at this point. It is not good to use random functions that rely on and modify the global state. Here is the best alternative I have found:
<?php
$post = get_queried_object();
?>
<div>
<?php echo do_shortcode(apply_filters('the_content', $post->post_content)); ?>
</div>
Upvotes: 0
Reputation: 41
You can achieve this by adding this simple php code block
<?php if ( have_posts() ) : while ( have_posts() ) : the_post();
the_content();
endwhile; else: ?>
<p>!Sorry no posts here</p>
<?php endif; ?>
Upvotes: 2
Reputation: 131
@Sydney Try putting wp_reset_query() before you call the loop. This will display the content of your page.
<?php
wp_reset_query(); // necessary to reset query
while ( have_posts() ) : the_post();
the_content();
endwhile; // End of the loop.
?>
EDIT: Try this if you have some other loops that you previously ran. Place wp_reset_query(); where you find it most suitable, but before you call this loop.
Upvotes: 13
Reputation: 3831
For people that don't like horrible looking code with php tags blasted everywhere...
<?php
if (have_posts()):
while (have_posts()) : the_post();
the_content();
endwhile;
else:
echo '<p>Sorry, no posts matched your criteria.</p>';
endif;
?>
Upvotes: 9
Reputation: 947
Page content can be displayed easily and perfectly this way:
<?php if(have_posts()) : ?>
<?php while(have_posts()) : the_post(); ?>
<h2><?php the_title(); ?></h2>
<?php the_content(); ?>
<?php comments_template( '', true ); ?>
<?php endwhile; ?>
<?php else : ?>
<h3><?php _e('404 Error: Not Found'); ?></h3>
<?php endif; ?>
Note:
In terms of displaying content - i) comments_template() function is an optional use if you need to enable commenting with different functionality.
ii) _e() function is also optional but more meaningful & effective than just showing text through <p>
. while preferred stylized 404.php can be created to be redirected.
Upvotes: 5
Reputation: 51
Just put this code in your content div
<?php
// TO SHOW THE PAGE CONTENTS
while ( have_posts() ) : the_post(); ?> <!--Because the_content() works only inside a WP Loop -->
<div class="entry-content-page">
<?php the_content(); ?> <!-- Page Content -->
</div><!-- .entry-content-page -->
<?php
endwhile; //resetting the page loop
wp_reset_query(); //resetting the page query
?>
Upvotes: 5
Reputation: 13910
This is more concise:
<?php echo get_post_field('post_content', $post->ID); ?>
and this even more:
<?= get_post_field('post_content', $post->ID) ?>
Upvotes: 32
Reputation: 9147
@Marc B Thanks for the comment. Helped me discover this:
<?php if ( have_posts() ) : while ( have_posts() ) : the_post();
the_content();
endwhile; else: ?>
<p>Sorry, no posts matched your criteria.</p>
<?php endif; ?>
Upvotes: 81