简体   繁体   中英

How to display the date underneath posts on home page?

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');

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM