• Neil Wilson

    (@neil-wilson)


    My site only has pages (no posts). I use a child 2012 Theme. I want to display the published date on the pages. How can this be accomplished without using a plugin?

Viewing 5 replies - 1 through 5 (of 5 total)
  • Moderator t-p

    (@t-p)

    threadi

    (@threadi)

    You could create a child theme and copy the page.php template to it. Then you adapt this copied file and add the output of the publication date at the place you want but within the while loop. Example

    $post_date = get_post_datetime(get_the_ID());
    echo $post_date->format('d/m/Y');

    Or you add this function in the functions.php of your child theme or via code snippet:

    function custom_page_date( $post ) {
     $post_date = get_post_datetime($post->ID);
     echo $post_date->format('d/m/Y');
    }
    add_filter( 'the_post', 'custom_page_date');

    However, here simply the date is then output above the content. You could refine this at this point and adjust it to your needs.

    If you don’t get along with the programming, you can also look for support for your specific case here: https://jobs.wordpress.net/

    Thread Starter Neil Wilson

    (@neil-wilson)

    threadi,

    I copied the 2012 page.php file to my child theme. I added the two lines of code you provided:

    $post_date = get_post_datetime(get_the_ID());echo
    $post_date->format(‘d/m/Y’);

    There was no change to the Pages (i.e. not date appears).

    The below snapshot is the page.php before changes.

    https://d.aafords.com/wp-content/uploads/zz-testing/page-PHP-file.jpg

    The below snapshot is the page.php with code inserted.

    https://d.aafords.com/wp-content/uploads/zz-testing/page-PHP-file-test.jpg

    Please let me know if I have inserted your code correctly. Thanks, Neil

    threadi

    (@threadi)

    You inserted it outside the code executed by PHP. Write the two lines instead of

    $post_date = get_post_datetime(get_the_ID());
    echo $post_date->format('d/m/Y');

    so

    <?php $post_date = get_post_datetime(get_the_ID());
    echo $post_date->format('d/m/Y'); ?>
    Thread Starter Neil Wilson

    (@neil-wilson)

    threadi,

    I tried inserting your new code in every location of page.php. Nothing worked.

    However, I was able to add code to the functions.php to get a date displayed on pages.

    Thanks for your help.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘How to add the published date to a page’ is closed to new replies.