22 Nov, 2004
Conditional text despite content not being echoed
Posted by: Jennifer In: WordPress: Lessons Learned
One of the things Christine had asked me to add to pixelog, was, when no "previous" entry was available – to have the text still show up, but be "greyed out". I remembered a post Alex King had made on the hackers mailing list about capturing the text that some functions typically echo into a variable. What he suggested was to do the following:
<?php
ob_start();
the_title();
$my_title = ob_get_contents();
ob_end_clean();
print($my_title);
?>
This was the trick I needed. Here's the code I used do the "greyed out" effect. (The issue here, if you don't know – is that next_post() will simply echo nothing if there is no next post)
<?php
//determine if there is a next post
ob_start();
next_post();
$nextpost = ob_get_contents();
ob_end_clean();
//now print the appropriate link or greyed out text
if ($nextpost) {
next_post('« % | ','previous','no');
} else { ?>
<span class="grey">« previous</span> |
<?php } ?>
(Yes I know I'm using the text "previous" with the next_post function… that had to do with her preference of displaying the photos)