Skip to Content

post_date()

Conveniently get the month, day, and year of a post in an organized way (with optional icon, and links to archive pages).

PHP March 25, 2020

Usage

PHP
nebula()->post_date($options)

Parameters

$options
(Optional) (Array) An array of options
Default: See below

Parameter Notes

label
Show a calendar “icon” or “text” (or nothing) next to the data
Default: icon

type
Use the date published, modified, or both.
Default: “published”

linked
Link each part of the date to an archive. This automatically disables if using a custom format.
Default: true

day
Include the day with the date
Default: true

relative
User relative dates (ex: “4 days ago”). This can be controlled globally by Customizer settings.
Default: true

format
Use a custom date format shown (meta attributes are not affected). Note: If using a custom format, the date cannot be linked.
Default: “F j, Y”

Request or provide clarification »

Examples

PHP
<?php echo nebula()->post_date(); ?>

Demo


Using post_meta:
Using individual Nebula post meta functions with options:
  • Posted in PHP by
  • Modified on
  • Custom Format:
Other Nebula meta functions:
  • Function
  • No EXIF data found

Source File

Located in /libs/Functions.php on line 462.

1 Hook

Find these filters and actions in the source code below to hook into them. Use do_action() and add_filter() in your functions file or plugin.

Filters
"nebula_post_date_defaults"
Need a new filter hook? Request one here.

Actions
This function has no action hooks available. Request one?

PHP
        public function post_date($options=array()){
            $defaults = apply_filters('nebula_post_date_defaults', array(
                'label' => 'icon', //"icon" or "text"
                'type' => 'published', //"published", or "modified"
                'relative' => get_theme_mod('post_date_format'),
                'linked' => true,
                'day' => true,
                'format' => 'F j, Y',
            ));

            $data = array_merge($defaults, $options);

            if ( $data['relative'] === 'disabled' ){
                return false;
            }

            //Apply the requested label
            $label = '';
            if ( $data['label'] == 'icon' ){
                $label = '<i class="nebula-post-date-label far fa-fw fa-calendar"></i> ';
            } elseif ( $data['label'] == 'text' ){
                $label = '<span class="nebula-post-date-label">' . esc_html(ucwords($data['type'])) . ' </span>';
            }

            //Use the publish or modified date per options
            $the_date = get_the_date('U');
            $modified_date_html = '';
            if ( $data['type'] === 'modified' ){
                $the_date = get_the_modified_date('U');
            }

            $relative_date = human_time_diff($the_date) . ' ago';

            if ( $data['relative'] ){
                return '<time class="posted-on meta-item post-date relative-date" title="' . date('F j, Y', $the_date) . '">' . $label . $relative_date . $modified_date_html . '</time>';
            }

            $day = ( $data['day'] )? date('d', $the_date) . '/' : ''; //If the day should be shown (otherwise, just month and year).

            if ( $data['linked'] && !isset($options['format']) ){
                return '<span class="posted-on meta-item post-date">' . $label . '<time class="entry-date" datetime="' . date('c', $the_date) . '" itemprop="datePublished" content="' . date('c', $the_date) . '"><a href="' . home_url('/') . date('Y/m', $the_date) . '/">' . date('F', $the_date) . '</a> <a href="' . home_url('/') . date('Y/m', $the_date) . '/' . $day . '">' . date('j', $the_date) . '</a>, <a href="' . home_url('/') . date('Y', $the_date) . '/">' . date('Y', $the_date) . '</a></time>' . $modified_date_html . '</span>';
            } else {
                return '<span class="posted-on meta-item post-date">' . $label . '<time class="entry-date" datetime="' . date('c', $the_date) . '" itemprop="datePublished" content="' . date('c', $the_date) . '">' . date($data['format'], $the_date) . '</time>' . $modified_date_html . '</span>';
            }
        }

Override

This function can not be short-circuited with an override filter. Request one?