1

The goal is very simple - to print page content on a page. I tried:

<?php the_content() ?>
<?= get_the_content() ?>

then

<?php the_content(get_the_ID()) ?>
<?= get_the_content(get_the_ID()) ?>

None of them worked.

Then I found this bizarre solution:

<?= apply_filters('the_content', get_post_field('post_content', get_the_ID() )) ?>

Why the_content() and get_the_content() didn't work?

Is there cleaner solution to display page content then the last one?

  • Please post the entire code of the page.php. Or, have a look at a nicer solution like _s with the actual part – Mayeenul Islam Dec 14 '15 at 11:59
3

A few notes before I start

  • NEVER EVER use php shortcodes, they are bad, confusing and not supported by default on some servers. IMHO, they should have never made it into PHP in the first place. WordPress Standards (which doesn't mean much) also "prohibits" the use of shorttags. Use proper php tags and forget that shorttags even exists

  • the_content() and get_the_content() does not accept the post ID, the firts parameter for both these template tags are $more_link_text

  • get_the_content() as well as the the content retrieved from the post_content field are unfiltered content, the_content filters are a set of filters that are applied to this unfiltered content and they include filters like wpautop which apply p tags to the_content()

Popular believe is that the_content() and get_the_content() MUST be used inside the loop. That is not entirely true, although it really is not recommended to use it outside the loop. Just like get_the_ID(), get_the_content() expects global post data to be set up ($post = get_post()), so if there are postdata available outside the loop in global scope, it will return the data.

Your issue is quite strange, if we look at source codes here, both get_the_ID() and get_the_content() calls $post = get_post(), and get_post() returns $GLOBALS['post'] if no ID is passed and if $GLOBALS['post'] are set, which seems to be the case here. get_post_field() successfully returns the page content according to you which means get_the_ID() returns the correct page ID that we are one, which means that get_the_content() should have worked and should have returned unfiltered page content (the_content() should have displayed filtered page content)

Anyways, you will need to debug this why things does not want to work as expected. To answer your question as to displaying page content without the loop correctly and reliably, you can access the queried object, get the $post_content property, apply the content filters to it and display the content. Just one note, this will not work if you are using query_posts in any way as it breaks the main query object which hold the queried object. This is the main reason why do must never ever use query_posts

$current_page = get_queried_object();
$content      = apply_filters( 'the_content', $current_page->post_content );
echo $content;
  • 1
    Wow, that's what I call an answer! 1) I know it's bad to use <? ... ?> kind of tags, but have also heard using <?= ... ?> are okay, and they don't require shortags enabled. 2) Yeah putting ids there was stupid of me...)) 3) I just tried the_content() returns nothing without the Loop. 4) I must add that I use page-about.php template and then use get_template_part('templates/content', 'about') function to get about page content. – Boykodev Dec 14 '15 at 13:09
  • Thanks for the feedback ;-). The old style <?php tags are still the prefered and recommended tag to use. Just a note on your 4th point, yes, that will be best, but requires the loop, which is what you wanted to avoid in the first place. ;-). – Pieter Goosen Dec 14 '15 at 13:24
  • I guess I made peace with using the Loop. ;-) – Boykodev Dec 14 '15 at 13:33
2

Displays the contents of the current post. This template tag must be within while loop.

<?php 
    if ( have_posts() ) {
        while ( have_posts() ) {
            the_post();
            the_content(); 
        } // end while
    } // end if
?>

More : the_content() - WordPress Codex

  • I just want to display the content of the current page. I don't want to use The Loop for this. – Boykodev Dec 14 '15 at 11:51
  • You must use the loop . – Danial Noaein Dec 14 '15 at 11:55
  • Ok, next question. Why does apply_filters() function worked without the loop? – Boykodev Dec 14 '15 at 11:56
  • Because apply_filters() is a WordPress built-in function and it rewrite new filters on default WordPress filters . – Danial Noaein Dec 14 '15 at 12:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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