Make WordPress Core

Opened 10 years ago

Closed 10 years ago

Last modified 10 years ago

#29732 closed defect (bug) (invalid)

post_template.php get_the_content() incorrect work when run without get_header?

Reported by: skynin's profile Skynin Owned by:
Milestone: Priority: normal
Severity: normal Version: 4.1
Component: Posts, Post Types Keywords:
Focuses: Cc:

Description

Hi!

I use AJAX in my theme, and use routing WP
The simple example:
file: single-mytypepost.php

        if ($_GET['ajax']) {
                echo get_the_content(); // 1
                die();
        }
        else {
                get_header();
                echo get_the_content();
                get_footer();
        }

don't work line 1, because
trace body get_the_content() - $pages is null!

My fix is in get_the_content():

    if (empty($pages)) { // fix, check global var $pages
        $content = $post->post_content;
    }
    else {
        if ( $page > count( $pages ) ) // if the requested page doesn't exist
            $page = count( $pages ); // give them the highest numbered page that DOES exist

        $content = $pages[$page - 1];
    }

Best regards,
Sergey (Skynin)

Change History (6)

#1 follow-up: @SergeyBiryukov
10 years ago

  • Component changed from Post Formats to Posts, Post Types

get_the_content() should only be used inside the loop. Looks like you're trying to call it separately.

#2 @Skynin
10 years ago

  • Resolution set to invalid
  • Status changed from new to closed

#3 @TobiasBg
10 years ago

  • Milestone Awaiting Review deleted

#4 in reply to: ↑ 1 ; follow-up: @Skynin
10 years ago

Replying to comment:1 SergeyBiryukov:

get_the_content() should only be used inside the loop. Looks like you're trying to call it separately.

Yes, I use outside the loop for a selected page. Global variable $post is initialized in this case, therefore i assumed that everything will work.

Thank you for the answer.

This ticket(#29732) can be removed.

#5 in reply to: ↑ 4 ; follow-up: @SergeyBiryukov
10 years ago

Replying to Skynin:

Global variable $post is initialized in this case, therefore i assumed that everything will work.

setup_postdata() would probably help.

#6 in reply to: ↑ 5 @Skynin
10 years ago

Replying to SergeyBiryukov:

setup_postdata() would probably help.

yes, it is! I was lazy. i'm sorry.
very thanx

Note: See TracTickets for help on using tickets.