I think you can use get_the_content(), append the date, and then call apply_filters on the result. This is UNTESTED, but it should be close:
$date = the_time('j-F-Y');
$content = get_the_content() . " $date. " ;
echo apply_filters('the_content',$content);
Hurm. Sadly that didn’t work. The content just comes out without the date.
I’ve been trying add_filter
instead of apply_filters
all along. Is there an easy way to explain the difference.
Thanks for having a look at this.
Terry
I’m sorry, I should have used get_the_time() instead of the_time().
add_filter registers a filter for a group with WP, to be called along with all the other filters for the same filter group.
apply_filters actually executes all the filter functions that have been registered.
Thanks for the apply vs. add explanation. I didn’t even realize there was an apply_filters
. Great.
Sadly again, however, even with get_the_time()
the code isn’t doing as it should.
To be completely clear, I have the following pasted as-is into functions.php
:
$date = get_the_time('j-F-Y');
$content = get_the_content() . " $date. " ;
echo apply_filters('the_content', $content);
But still, it doesn’t modify anything.
Appreciate the help very much,
Terry
Some of the code does work, however.
If I put this in instead:
$test_string = "Test string to see if the filter works";
$content = get_the_content() . $test_string ;
echo apply_filters('the_content', $content);
It prints the test string right at the top of the page.
A ha! Interestingly, when I wrap it in php tags and put it in sidebar.php
it finally works. Breakthrough!
More experimentation and I’ll post back proper results.
Thanks.
The code I gave you should be a replacement for calling the_content() in the template used to display the page, not a function to go in functions.php.
Thanks a million @vtxyzzy. Works perfectly now. This also gave me a great opportunity to learn about filters! So thanks for that too.
As if I haven’t asked enough questions today, if you know how I might turn the date into a clickable permalink, that would be A++!
I’ve tried a few versions of this, but none have worked so far.
<?php
$date = get_the_time('d-m-y');
$content = get_the_content() . "<a href="<?php the_permalink(); ?>">$date</a>" ;
echo apply_filters('the_content',$content);
?>
I’d hoped that by just putting the permalink in before the date and turning it all into a link it would work….but no avail.
If you have any ideas, I’d love to hear them, otherwise, thanks a whole lot for helping with this today.
Terry
I suspect this is another one of those easily explainable things.
If I try just a plain-text string in before $date
it works fine. Like so:
<?php
$date = get_the_time('d-m-y');
$content = get_the_content() . "testing" . $date;
echo apply_filters('the_content',$content);
?>
But trying to put a link in place of the string “testing” doesn’t work:
<?php
$date = get_the_time('d-m-y');
$content = get_the_content() . "<a href="#">test link</a>" . $date;
echo apply_filters('the_content',$content);
?>
So there must be something funny about not being able to put hypertext in there?
My guess is that the filters are stripping out the link. There may be a better way to do this, but give this a try:
<?php
$date = get_the_time('d-m-y');
$content = get_the_content() . '$$$';
$content = apply_filters('the_content',$content);
$content = str_replace('$$$'," <a href='#'>test link</a> " . $date);
echo $content;
?>