Skip to content

Commit e7cc029

Browse files
authored
Merge pull request #13 from OpenSourceOrg/add/9-last-modified-date
Added the created and last modified date to pages
2 parents 6a8a556 + 6675999 commit e7cc029

File tree

3 files changed

+33
-10
lines changed

3 files changed

+33
-10
lines changed

themes/osi/inc/template-functions.php

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -169,15 +169,14 @@ function get_license_search_query() {
169169
$query = sanitize_text_field( $query );
170170

171171
return $query;
172-
173172
}
174173
}
175174

176175

177176
// Meta Block Field Filter Date
178177
add_filter( 'meta_field_block_get_block_content', 'osi_filter_meta_block_date_output', 10, 4 );
179178
function osi_filter_meta_block_date_output( $content, $attributes, $block, $post_id ) {
180-
if ( is_numeric( $content ) && 8 == strlen( $content ) ) {
179+
if ( is_numeric( $content ) && 8 === strlen( $content ) ) {
181180
$content = DateTime::createFromFormat( 'Ymd', $content )->format( 'M Y' );
182181
}
183182
return $content;
@@ -187,17 +186,37 @@ function osi_filter_meta_block_date_output( $content, $attributes, $block, $post
187186
add_action( 'pre_get_posts', 'osi_press_mentions_by_publication_date' );
188187
function osi_press_mentions_by_publication_date( $query ) {
189188
if ( ! is_admin() && $query->is_main_query() ) {
190-
if( is_post_type_archive( 'press-mentions' ) ) {
189+
if ( is_post_type_archive( 'press-mentions' ) ) {
191190
$query->set( 'meta_key', 'date_of_publication' );
192191
$query->set( 'orderby', 'meta_value_num' );
193-
$query->set( 'order', 'DESC');
194-
$query->set( 'meta_query', array(
192+
$query->set( 'order', 'DESC' );
193+
$query->set(
194+
'meta_query',
195195
array(
196-
'key' => 'date_of_publication',
197-
'type' => 'numeric',
198-
)
199-
) );
196+
array(
197+
'key' => 'date_of_publication',
198+
'type' => 'numeric',
199+
),
200+
)
201+
);
200202
}
201203
}
202204
return $query;
203-
}
205+
}
206+
207+
/**
208+
* Renders the "Created" and "Last modified" string for a page.
209+
*/
210+
211+
function osi_the_page_dates() {
212+
if ( ! is_home() && ! is_front_page() ) {
213+
$max_date = '2023-02-01'; // February 1, 2023
214+
$created = get_the_date( 'F j, Y' );
215+
$modified = get_the_modified_date( 'F j, Y' );
216+
217+
if ( strtotime( $created ) < strtotime( $max_date ) ) {
218+
// Post was created before the 'max date'.
219+
echo sprintf( '<h4 class="page_dates">Page created on %1$s | Last modified on %2$s</h4>', esc_html( $created ), esc_html( $modified ) );
220+
}
221+
}
222+
}

themes/osi/template-parts/content-page-no-header.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,6 @@
1414
<?php the_content(); ?>
1515
</div><!-- .entry-content -->
1616

17+
<?php osi_the_page_dates(); ?>
18+
1719
</article><!-- #post-<?php the_ID(); ?> -->

themes/osi/template-parts/content-page.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,6 @@
1515
<?php the_content(); ?>
1616
</div><!-- .entry-content -->
1717

18+
<?php osi_the_page_dates(); ?>
19+
1820
</article><!-- #post-<?php the_ID(); ?> -->

0 commit comments

Comments
 (0)