0

I'm working in wordpress using a theme that has a custom function to build a calendar page. This allows me to create a page with introductory text followed by events listings underneath.

The listings allow the user to view the 'Prev' or 'Next' months events, but doing so causes the introductory text to disappear although the URL doesn't change.

I've found the function in the theme files and tried editing it as below:

  if($action_identifier == 'get_cal'){
    $monthdata = explode('/', $thedata);
    $month = $monthdata[0];
    $year = $monthdata[1];
    $content = '<div id="post" class="page">';
    $content .='<div class="introduction">';   /* these */
    $content .= the_content();                 /* lines */
    $content .='</div>';                       /* added */
    $content .= '<div class="calmonth clear darkbox">';
    $content .= prevlink($month , $year);
    $content .= '</div>';
    $content .= '</div><div class="calentries">';
    $content .= get_the_calendar($month,$year) . '</div>';  
    $output = $content; 

}

What I'm attempting here is to create the 'introduction' div and then use the wordpress function 'get_content' to display the page content in the div.

This semi works in that it places the 'introduction' div where I want it, but doesn't display the content (it will display text when I type something in instead of 'the_content()' ). I think this is likely a syntax issue, because I'm an utter novice - can anyone give me any suggestions?

Thanks in advance.

2
  • does your the_content() return content..! Commented Mar 15, 2012 at 12:18
  • in the main page template, yes it does, but not in this example. will it cause an issue if this function is called in the page template after the initial call to 'get_content'? Commented Mar 15, 2012 at 12:19

4 Answers 4

2

the_content() prints the content. Use get_the_content()

$content .= get_the_content();                 /* lines */
Sign up to request clarification or add additional context in comments.

6 Comments

Try adding var_dump(get_the_content()); and post the result.
Thanks - it returns ' string(0) "" ' Does that mean anything to you?!
Yeah, it just has some introductory text before the calendar displays underneath. I had to add <?php the_content(); ?> into the page template to display this text though, as the calendar page template in the theme ignores the the content otherwise. I guess this could be creating issues..
Hey, if I try var_dump(the_content()); it returns NULL - so it's saying that there is no content?
@SamCampsall Don't use the_content(), use get_the_content() instead! That's what the answers are all about.
|
0

Wordpress is strange in these things; it will echo the content from inside the function, so string concatenation is normally out of the question. Most of the the functions also have a counterpart that starts with get_. This is also the case of the the_content function, so replacing your call with $content .= get_the_content(); would solve your issue.

That said, as an extension to my original answer; if you run into a function that doesn't have a get_ alternative, you might want to use output buffering to make sure the function doesn't output everything:

<?php
function the_content( ) {
   echo "Foo";
}

ob_start( );
the_content( );
$content = ob_get_clean( );

doSomethingWith( $content ); // $content is now "Foo".

1 Comment

Thanks for the response - but see below - I've tried this and it doesn't do the trick
0

Use an output buffer.

ob_start();
the_content();
$buffered_content = ob_get_clean();

if($action_identifier == 'get_cal'){
    $monthdata = explode('/', $thedata);
    $month = $monthdata[0];
    $year = $monthdata[1];
    $content = '<div id="post" class="page">';
    $content .='<div class="introduction">';   /* these */
    $content .= $buffered_content              /* lines */
    $content .='</div>';                       /* added */
    $content .= '<div class="calmonth clear darkbox">';
    $content .= prevlink($month , $year);
    $content .= '</div>';
    $content .= '</div><div class="calentries">';
    $content .= get_the_calendar($month,$year) . '</div>';  
    $output = $content; 
}

4 Comments

I'm not entirely sure how to code that in. I added ob_start(); the_content(); after the opening curly brace, and then changed the_content() to ob_get_clean(). This didn't work, but I don't know if that's cos I coded it wrong...
Thanks for the edit - I've tried it as you suggested but no dice :( I don't know if the below comments are related - it seems that 'the_content' may just be returning empty, although I'm not sure why.. thanks for your help either way
Hey Sam, if both Dogbert and my solution don't work we know for sure that no content is being delivered and something is wrong within the_content. btw, the_content doesn't return strings anyway, it writes them.
Yeah, this seems to be the problem! Sorry for wasting everyones time! :S
0

It sounds like the $post object is outside the scope of that function. If it is you will have to pass that object to the function, or pass an ID then get the post object from that, or globalize $post. If it is within the scope of that function try using

$post->post_content

Another thing is the calendar may be using AJAX. If that is the case than you cannot globalize the $post. You will have to pass and ID or $post oject.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.