Skip to content

Commit faa4899

Browse files
carolinanaristath
andauthored
Only display the modified post date if the post has been modified (#46839)
* Only display the modified post date if the post has been modified * Add wp-block-post-date__modified-date to the $classes array * Update index.php * Update packages/block-library/src/post-date/index.php Co-authored-by: Ari Stathopoulos <[email protected]> --------- Co-authored-by: Ari Stathopoulos <[email protected]>
1 parent f20e276 commit faa4899

File tree

2 files changed

+21
-8
lines changed

2 files changed

+21
-8
lines changed

packages/block-library/src/post-date/edit.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ export default function PostDateEdit( {
3737
const blockProps = useBlockProps( {
3838
className: classnames( {
3939
[ `has-text-align-${ textAlign }` ]: textAlign,
40+
[ `wp-block-post-date__modified-date` ]: displayType === 'modified',
4041
} ),
4142
} );
4243

@@ -175,6 +176,9 @@ export default function PostDateEdit( {
175176
} )
176177
}
177178
checked={ displayType === 'modified' }
179+
help={ __(
180+
'Only shows if the post has been modified'
181+
) }
178182
/>
179183
</PanelBody>
180184
</InspectorControls>

packages/block-library/src/post-date/index.php

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,25 +18,34 @@ function render_block_core_post_date( $attributes, $content, $block ) {
1818
return '';
1919
}
2020

21-
$post_ID = $block->context['postId'];
21+
$post_ID = $block->context['postId'];
22+
$formatted_date = get_the_date( empty( $attributes['format'] ) ? '' : $attributes['format'], $post_ID );
23+
$unformatted_date = esc_attr( get_the_date( 'c', $post_ID ) );
24+
$classes = array();
2225

23-
$classes = array();
2426
if ( isset( $attributes['textAlign'] ) ) {
2527
$classes[] = 'has-text-align-' . $attributes['textAlign'];
2628
}
2729
if ( isset( $attributes['style']['elements']['link']['color']['text'] ) ) {
2830
$classes[] = 'has-link-color';
2931
}
30-
$wrapper_attributes = get_block_wrapper_attributes( array( 'class' => implode( ' ', $classes ) ) );
3132

33+
/*
34+
* If the "Display last modified date" setting is enabled,
35+
* only display the modified date if it is later than the publishing date.
36+
*/
3237
if ( isset( $attributes['displayType'] ) && 'modified' === $attributes['displayType'] ) {
33-
$formatted_date = get_the_modified_date( empty( $attributes['format'] ) ? '' : $attributes['format'], $post_ID );
34-
$unformatted_date = esc_attr( get_the_modified_date( 'c', $post_ID ) );
35-
} else {
36-
$formatted_date = get_the_date( empty( $attributes['format'] ) ? '' : $attributes['format'], $post_ID );
37-
$unformatted_date = esc_attr( get_the_date( 'c', $post_ID ) );
38+
if ( get_the_modified_date( 'Ymdhi', $post_ID ) > get_the_date( 'Ymdhi', $post_ID ) ) {
39+
$formatted_date = get_the_modified_date( empty( $attributes['format'] ) ? '' : $attributes['format'], $post_ID );
40+
$unformatted_date = esc_attr( get_the_modified_date( 'c', $post_ID ) );
41+
$classes[] = 'wp-block-post-date__modified-date';
42+
} else {
43+
return '';
44+
}
3845
}
3946

47+
$wrapper_attributes = get_block_wrapper_attributes( array( 'class' => implode( ' ', $classes ) ) );
48+
4049
if ( isset( $attributes['isLink'] ) && $attributes['isLink'] ) {
4150
$formatted_date = sprintf( '<a href="%1s">%2s</a>', get_the_permalink( $post_ID ), $formatted_date );
4251
}

0 commit comments

Comments
 (0)