0

new to coding. Thanks in advance for your help :)

I've gotten the date to display on posts after they've been clicked (one post on a single page) but it won't show up on the main page with all the posts.

How do I apply it to posts on the main/front/home page? How do I modify it in CSS? (change color or font, for instance)

This is in the functions.php:

function add_after_post_content($content) {
    if(!is_feed() && !is_front_page() && !is_home() && is_singular() && is_main_query()) {
        $content .= '<p> Posted '.date('F j, Y').'&nbsp;'.'</p>';
    }
    return $content;
}
add_filter('the_content', 'add_after_post_content');
1
  • 1
    are you talking about Wordpress? You should mention that in your question and add it as a tag...
    – Johannes
    Jul 21, 2016 at 21:40

1 Answer 1

0

For the PHP in functions.php, try changing to this:

function add_after_post_content($content) {
    if(!is_feed() && is_singular() && is_main_query()) {
        $content .= '<p> Posted '.date('F j, Y').'&nbsp;'.'</p>';
    }
    return $content;
}
add_filter('the_content', 'add_after_post_content');

Basically, the second line of that function uses !is_front_page() && !is_home() to say "if NOT the home page and NOT the front page then execute this code", !is_home being the way PHP/Wordpress checks if something is NOT true.

For the styling, you can add a class to the <p> tag in your code, like this:

$content .= '<p class="my-date-style"> Posted '.date('F j, Y').'&nbsp;'.'</p>';

Then add this to your CSS file:

.my-date-style {
  color: blue;
}

Without seeing the rest of the code it's difficult to say for sure, but this should get you headed in the right direction.

1
  • Definitely in the right direction! Couple of adjustments made possible by you: changed .date to .get_the_date (before it was just displaying today's date, which I didn't notice before); took away !is_front_page() && !is_home() but still wasn't working... then I took away is_singular() and it worked; and your styling advice worked! Thanks for your help :)
    – Alexander
    Jul 22, 2016 at 23:39

Your Answer

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

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